How To Make Login Page For Different User In Php

In this article, I will guide you through the process of creating a login page for different users using PHP. As a web developer myself, I understand the importance of having a secure and user-friendly login system for any website. So, let’s dive in and explore the steps involved in building a robust login page.

Step 1: Setting Up the Database

The first step is to create a database to store user information. You can use a relational database management system like MySQL or MariaDB. Create a table named “users” with columns such as “id”, “username”, “password”, and “role”. The “role” column will help us differentiate between different users. For example, you can have roles like “admin”, “customer”, or “guest”.

Here’s an example of how you can create the users table:

CREATE TABLE users (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
role VARCHAR(20) NOT NULL
);

Step 2: Creating the Login Form

Now, let’s create the login form. In your HTML file, add a form element with fields for username and password. Make sure to set the form’s method to “POST” and the action to the PHP file where we’ll handle the login process.

<form method="POST" action="login.php">
<div>
<label for="username">Username:</label>
<input type="text" name="username" id="username" required>
</div>
<div>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required>
</div>
<button type="submit">Login</button>
</form>

Step 3: Handling the Login Process

In the PHP file specified in the form’s action attribute, we need to handle the login process. Start by connecting to the database and retrieving the user’s entered username and password from the POST data.

Next, use a SQL query to check if the username and password match any user in the database. If a match is found, verify the user’s role and redirect them to the appropriate page based on their role. For example, you can redirect admins to the admin dashboard, customers to their profile, and guests to a welcome page.

<?php
// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database_name");

// Get the user's entered username and password
$username = $_POST['username'];
$password = $_POST['password'];

// Query to check if the username and password match
$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);

// Check if a user was found
if ($row) {
// Redirect users based on their role
$role = $row['role'];
if ($role == 'admin') {
header("Location: admin_dashboard.php");
exit();
} elseif ($role == 'customer') {
header("Location: customer_profile.php");
exit();
} else {
header("Location: welcome.php");
exit();
}
} else {
echo "Invalid username or password.";
}
?>

Conclusion

Creating a login page for different users in PHP requires setting up a database, designing the login form, and handling the login process. By following the steps outlined in this article, you can build a secure and efficient login system for your website. Remember to always handle user authentication securely to protect sensitive user information. Happy coding!