Introduction
In the previous tutorial, we created the User Registration System that allows new users to create accounts and store their information in the database.
In this part of the project, we will build the User Login System using PHP and MySQL. The login system allows registered users to access their accounts by verifying their email and password.
Login systems are used in almost every modern website, including e-commerce platforms, social networks, membership websites, and learning portals.
During the login process, the system checks whether the user’s credentials match the records stored in the database. If the credentials are correct, the system allows the user to access protected pages.
By the end of this tutorial, users will be able to log in securely using their registered email and password.
What We Will Do in This Part
In this tutorial, we will implement the login functionality of the authentication system.
The login system will include the following features:
- Login form for user authentication
- Retrieving user data from the database
- Verifying passwords using
password_verify() - Starting a session after successful login
- Redirecting the user to a protected page
This will allow registered users to log in and access their dashboard.
How the Login System Works
Before writing the code, it is useful to understand how the login process works.
The typical workflow of a login system is:
- The user enters their email and password.
- The form data is sent to the server using the POST method.
- PHP searches the database for the user with that email.
- The stored password hash is retrieved.
- PHP compares the entered password with the stored hash using
password_verify(). - If the password is correct, a session is created.
- The user is redirected to a protected page such as a dashboard.
This process ensures that only authorized users can access restricted areas of the website.
Creating the Login Form
First, create a new file named:
login.php
Add the following HTML code to create the login form.
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
</head>
<body>
<h2>User Login</h2>
<form method="POST" action="">
Email:<br>
<input type="email" name="email" required><br><br>
Password:<br>
<input type="password" name="password" required><br><br>
<input type="submit" name="login" value="Login">
</form>
</body>
</html>
This form collects the email and password from the user.
Connecting to the Database
Next, include the database connection file.
Add the following code at the top of login.php.
<?php
include "db.php";
?>
This allows the login script to access the users table in the database.
Checking if the Form Was Submitted
When the user clicks the login button, the form data is sent to the server.
Add the following PHP code:
<?phpif(isset($_POST['login'])){$email = $_POST['email'];
$password = $_POST['password'];}
?>
This retrieves the email and password entered by the user.
Retrieving the User from the Database
Next, we search the database for the user with the provided email address.
$stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
This query retrieves the user record from the users table.
Verifying the Password
After retrieving the user data, we verify the password using password_verify().
if($user && password_verify($password, $user['password'])){
echo "Login successful";
}else{
echo "Invalid email or password";
}
The password_verify() function compares the entered password with the hashed password stored in the database.
If the password matches, the user is authenticated successfully.
Starting a Session After Login
After a successful login, we should create a PHP session to keep the user logged in.
Add the following code:
session_start();
$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['name'];
header("Location: dashboard.php");
exit();
This creates a session and redirects the user to the dashboard page.
Sessions allow websites to remember that the user is logged in.
Complete Login System Code
Below is the complete login.php script.
<?php
include "db.php";
session_start();if(isset($_POST['login'])){$email = $_POST['email'];
$password = $_POST['password'];$stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();$result = $stmt->get_result();
$user = $result->fetch_assoc();if($user && password_verify($password, $user['password'])){$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['name'];header("Location: dashboard.php");
exit();}else{echo "Invalid email or password";}}
?>
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
</head>
<body><h2>User Login</h2><form method="POST">Email:<br>
<input type="email" name="email" required><br><br>Password:<br>
<input type="password" name="password" required><br><br><input type="submit" name="login" value="Login"></form></body>
</html>
This script authenticates users and redirects them to the dashboard after a successful login.
Testing the Login System
To test the login functionality:
- Start your local server.
- Open your browser.
- Navigate to:
http://localhost/login-system/login.php
- Enter the email and password of a registered user.
- Click the Login button.
If the credentials are correct, the user will be redirected to the dashboard page.
Common Beginner Mistakes
Here are some common mistakes beginners make when building login systems:
- Forgetting to start the session using
session_start() - Not hashing passwords during registration
- Using incorrect database connection details
- Not verifying whether the user exists
- Forgetting to redirect users after login
Avoiding these mistakes will help your login system function correctly.
Practice Tasks
Try these tasks to practice what you learned.
- Task 1: Create the
login.phpfile. - Task 2: Build the login form using HTML.
- Task 3: Retrieve user data from the database using prepared statements.
- Task 4: Verify the password using
password_verify(). - Task 5: Create a session and redirect the user to the dashboard.
Conclusion
In this tutorial, we built the User Login System using PHP and MySQL. We created a login form, retrieved user data from the database, verified the password using password_verify(), and started a session after successful authentication.
The login system allows registered users to securely access their accounts and interact with protected pages on the website.
In the next tutorial, we will implement Logout and Session Management, which will allow users to safely log out of their accounts and end their sessions.
