Log In Process

Create Login with PHP and MySQL
Create Login with PHP and MySQL : Home
Algorithm
Sign Up Process
Log In Process
Log Out Process

Log In
We will save some time here and will only create one file log-in.php. The process is same as Sign Up : We will check if POST variables are set else we will show Log In form.

<?php
// Starting Session
session_start();

// Checkinng if we have data from login form

if(isset($_POST['login-email'])&&isset($_POST['login-password'])){
	
	// Converting any special characters to entities and email to lowercase
	// So that same email is not considered different

	$email = htmlspecialchars(strtolower($_POST['login-email']));
	// Encrypting Password
	$password = md5($_POST['login-password']);

	$mysqli_object= new mysqli("localhost","","","db");
	$sql_login = "SELECT * FROM users WHERE email='".$email."' AND password='".$password."'";
	$result_login = $mysqli_object->query($sql_login);

	if($result_login->num_rows>0){
		$row=$result_login->fetch_assoc();
		//Using data from database
		// Storing data in database
		$_SESSION['user-email'] = $row['email'];
		$_SESSION['user-name'] = $row['name'];
		echo "Congrats ".$_SESSION['user-name'];
		echo "You are Logged In We promised you something here it is <br><b>Secret Data</b>";
	}
	else{
		echo "Wrong Credentials <a href='log-in.php'>Login again</a>";
	}

	

}
// If POST data is not available i.e. form is not submitted
else{
	// Embedding HTML in PHP
	?>
<html>
<body>
<form method="post" action="log-in.php">
	<input type="text" name="login-email" placeholder="email" required="required">
	<input type="password" name="login-password" placeholder="password" required="required">
	<input type="submit" name="submit">
</form>

</body>
</html>
	<?php

}
?>

This code works perfectly fine, but what if user is already Logged In. This code will simply throw Log In form to that user. So we need to get this solved as well.
We will check for $_SESSION['user-email'] and $_SESSION['user-name']. This should solve the problem.

We are adding another page b-page.php as well to demostrate use of session data in multiple pages.

<?php
// Starting Session
session_start();
if(isset($_SESSION['user-email'])&&isset($_SESSION['user-name'])){
	echo "".$_SESSION['user-name'];
	echo "You are already Logged In<br> We promised you something here it is <br><b>Secret Data</b><br>";
	echo "<a href='b-page.php'>b-page</a>";
}
else{
// Checkinng if we have data from login form

if(isset($_POST['login-email'])&&isset($_POST['login-password'])){
	
	// Converting any special characters to entities and email to lowercase
	// So that same email is not considered different

	$email = htmlspecialchars(strtolower($_POST['login-email']));
	// Encrypting Password
	$password = md5($_POST['login-password']);

	$mysqli_object= new mysqli("localhost","","","db");
	$sql_login = "SELECT * FROM users WHERE email='".$email."' AND password='".$password."'";
	$result_login = $mysqli_object->query($sql_login);

	if($result_login->num_rows>0){
		$row=$result_login->fetch_assoc();
		//Using data from database
		// Storing data in database
		$_SESSION['user-email'] = $row['email'];
		$_SESSION['user-name'] = $row['name'];
		echo "Congrats ".$_SESSION['user-name'];
		echo "You are Logged In We promised you something here it is <br><b>Secret Data</b><br>";
		echo "<a href='b-page.php'>b-page</a>";
	}
	else{
		echo "Wrong Credentials <a href='log-in.php'>Login again</a>";
	}

	

}
// If POST data is not available i.e. form is not submitted
else{
	// Embedding HTML in PHP
	?>
<html>
<body>
<form method="post" action="log-in.php">
	<input type="text" name="login-email" placeholder="email" required="required">
	<input type="password" name="login-password" placeholder="password" required="required">
	<input type="submit" name="submit">
</form>

</body>
</html>
	<?php

}
}
?>

Login to Track Your Progress