If you are working on a web development project where users can register and login then you must create a secure login system for your website so that you can allow users to login to your website without any hassle. We can create a login system in PHP very easily, and we’ll need to have a MySQLi database and table in order to save the user information such as user name, user email and user password etc. After we have a table to store users then we can create a script in PHP for verifying the users who want to sign in to our website. Let’s do it in PHP.
Create the Table for Users
The first step for creating such system in PHP is to create a database inside PHPMyAdmin and after you create a database with the name “users”, after that simply create a new table named “users” in that database & add only 4 columns. You can give the following names to your table’s columns:
After creating the table & adding the column names, simply find the “Insert Option” there, and insert two or three test users to that table. When you complete this task, then you can create a simple sign in form to verify the users and let them sign in to our main page of the website.
Watch this video tutorial first!
Create the Login Script in PHP
When we create a registration form, we may ask users to enter their full details such as name, email, password, country, date of birth etc, but when we create the login form, we only need two things, either user name & password or email & password. So here we’ll create a simple HTML form for user_email and user_password, and after the user enters their details, we’ll look into our table, if his/her details are existing, we’ll send them to the main page, else we’ll give them an error in php. The HTML login form will be something like this:
<!DOCTYPE html><?php session_start();?>
<form action=”login.php” method=”post”>
<table width=”500″ align=”center” bgcolor=”skyblue”>
<td colspan=”3″><h2>User Login</h2></td>
<td><input type=”text” name=”email” required=”required “/></td>
<td><input type=”password” name=”pass” required=”required></td>
<input type=”submit” name=”login” value=”Login”/>
Using the above simple HTML form, we’ll get the user email and user password, we’ve given the appropriate names to the <input> tags which we can target in PHP using the $_POST method, so we’ll get the values from the <input> fields and then we’ll check whether we have that user in the database or not, so after validation, if the user is existing, we’ll allow them to enter the site, else we’ll add an error in PHP.
The PHP script for this login system will be something like this:
// establishing the MySQLi connection
$con = mysqli_connect(“localhost”,”root”,””,”users”);
echo “MySQLi Connection was not established: ” . mysqli_connect_error();
// checking the user
$email = mysqli_real_escape_string($con,$_POST[’email’]);
$pass = mysqli_real_escape_string($con,$_POST[‘pass’]);
$sel_user = “select * from users where user_email=’$email’ AND user_pass=’$pass'”;
$run_user = mysqli_query($con, $sel_user);
$check_user = mysqli_num_rows($run_user);
echo “<script>alert(‘Email or password is not correct, try again!’)</script>”;
Additionally, we’ll can use $_SESSION Array in PHP to end of the session of the user when they want to logout from our website. so you must add a session_start() in the starting of the HTML page which I’ve done in the login page. This will store the user session if the user has logged in successfully. I’ve registered the session of the user if the login is successful, you can find $_SESSION[‘user_email’]=$email in the PHP code above, that will create a new session for the user and every user will have a new session automatically.
Creating the Logout PHP Script
Now after the user is logged in, you can simply add a “Logout” link to the welcome page, and when a user clicks that link, he/she will be redirected to logout.php, and logout.php script will be something like this:
So the above simple logout page will destory or end up the session of the user, and will redirect him/her to the login.php where they’ll have to enter their details again if they want to login again same as Facebook.
It’s end up here, if you have questions then do let me know in the comments, I’ll be glad to help you out!