• Register
278 points
6 1

Here, I’ll explain how to create a login page in php and mysql with session.

Building the Login System with Session

In this section we'll create a login form where user can enter their email and password. When user submit the form, these inputs will be verified against the credentials stored in the database, if the email and password match, the user is authorized and granted access to the next page, otherwise the login attempt will be rejected. 
But, first we need to create a table that will hold all the user data.

 

Step 1: Creating the table named as users in the demo database:-

CREATE TABLE users ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
                                       email VARCHAR(50) NOT NULL UNIQUE, 
                                       password VARCHAR(255) NOT NULL);
idemailpassword
1abc@gmail.comabc123
2def@gmail.com567def
3jvp@gmail.com26jvpl21


Step 2: Creating the Connection File:-

After creating the table, we need to create a PHP script in order to connect to the MySQL database server. Let's create a file named "connection.php".

<?php
	$con = mysqli_connect("localhost","root","root","demo");
	if (mysqli_connect_errno()) 
	{ 
		echo "Failed to connect to MySQL:" . mysqli_connect_error(); 
	}
	else
	{ 
		echo "Connected to MySQL"; 
	}
?>

Step 3: Creating the Login Form:-

Let's create a file named "login.php"

<?php
	session_start();
	if(isset($_SESSION['email']))
	{
		header("location:welcome_page.php");
	}
	else
	{
		include("connection.php");
		$msg="";
		if(isset($_POST['login']))
		{
			$email=$_POST['email'];
			$password=$_POST['password'];

			$result=mysqli_query($con,"SELECT password FROM users WHERE email='$email' and password='$password'"); 
			$retrievepassword=mysqli_fetch_assoc($result);
			$row=mysqli_num_rows($result);
			
			if($row > 0)
			{
				$_SESSION['email']=$email;
				header("Location:welcome_page.php");
			}
			else
			{
				$msg="INVALID EMAIL OR PASSWORD";
			}
		}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Login Form</title>
</head>
<body>
     <form name="login" id="login" method="post" action="login.php">
        <h2 align="center">LOGIN</h2>
        <div id="error" style="color:red;"><?php echo $msg; ?></div>
        <label for="email">Email</label>
        <input type="text" name="email" id="email"/>
        <label for="password">Password</label>
        <input type="password" name="password" id="password"/>
        <input type="submit" name="login" value="LOGIN"/>
     </form>
</body>
</html>
<?php
	}
?>

Step 4: Creating the Welcome Page:-

Let’s create a "welcome.php" file, where user is redirected after successful login.
<?php
// Initialize the session
session_start();
 
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["login"]) || $_SESSION["login"] !== true){
    header("location: login.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></b>. Welcome !!!.</h1>
</body>
</html>

Step 5: Creating a Logout Script:-

Now, let's create a "logout.php" file. When the user clicks on the log out or sign out link, the script inside this file destroys the session and redirect the user back to the login page.
<?php
// Begin the session
session_start();

// Unset all of the session variables.
session_unset();

// Destroy the session.
session_destroy();

//Redirect to Login Page
header("Location:login.php");
?>

 

4 Comments

comment:

thank you for sharing this

Comment:

thank you for your help

Comment:)

Hey jeevani!Great struggle ,This article is full of knowledge and motivation. Good post .Thank you for sharing this. yes...

Today you have taught me something new. Great job. Continue to post :)