In this post we will learn how to make login form with remember me functionality in PHP. What is Remember Me and why we need it? Basically when user check the remember me button the user will be logged into your website even if he close the browsers or shutdown the computer and the user will only be logged out when he click the logout button.



login form with remember me

Html markup

We have nothing special in the markup just a regular old form with remember me checkbox.

<form action="login.php" method="post">
		<p><input type="text" name="username" placeholder="Enter your username"></p>
		<p><input type="password" name="password" placeholder="Enter your password"></p>
		<p><input type="checkbox" name="re" id="re" value="on"> <label for="re">Remember Me</label></p>
		<input type="submit" name="login" value="login">
	</form>

Database

Create a databse with name login_remember and a table of user

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL,
  `username` text NOT NULL,
  `password` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

INSERT INTO `user` (`id`, `username`, `password`) VALUES
(1, 'husain', 'husain');

ALTER TABLE `user`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `user`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;

Connect to the database

<?php
	//connect to db
	$con = mysqli_connect("localhost","root","","login_remember") or die(mysqli_error());
	?>

Login Script

  1. On line 1 we are waiting for the form to submit
  2. On line 6 we are checking if the $username or $password is empty we will display a message and exit the script
  3. On line 12 we are checking that the remember me button is checked or not. If Remember Me button is checked we set the $re=”on” else we set $re=””
  4. on line 19 we are making a query to the database
  5. on line 21 we are checking if query return one result means the username and password are correct
  6. on line 24 we check if $re=”on” means user has checked remember me button so we will start cookies else we will start session
<?php
	if(isset($_POST&#91;'login'&#93;)){
		$username = $_POST&#91;'username'&#93;;
		$password = $_POST&#91;'password'&#93;;
		
		if(empty($username) OR empty($password)){
			echo "Fill in all the fields";
			exit();
		}

		//check remeber me button is set or not
		if(isset($_POST&#91;'re'&#93;)){
			$re = "on";
		}else{
			$re = "";
		}

		//check username and pass
		$query = mysqli_query($con,"SELECT * FROM `user` WHERE username='$username' AND password='$password'");

		if(mysqli_num_rows($query) == 1){

			//login the user in
			if($re == "on"){ //remember me checked
				setcookie("username",$username,time() + (86400  * 10));
			}else{ //remember me not checked
				session_start();
				$_SESSION&#91;'username'&#93; = $username;
			}

			echo "user logedin";
			header("Location: index.php");
			exit();
		}

		echo "Invalid username and password";
		exit();

	}
?>

Login.php – Putting it all together

<html>
<head>
	<title>Login</title>
</head>
<body>
	<?php
		//connect to db
		$con = mysqli_connect("localhost","root","","login_remember") or die(mysqli_error());
	?>
	<form action="login.php" method="post">
		<p><input type="text" name="username" placeholder="Enter your username"></p>
		<p><input type="password" name="password" placeholder="Enter your password"></p>
		<p><input type="checkbox" name="re" id="re" value="on"> <label for="re">Remember Me</label></p>
		<input type="submit" name="login" value="login">
	</form>
	<?php
		if(isset($_POST&#91;'login'&#93;)){
			$username = $_POST&#91;'username'&#93;;
			$password = $_POST&#91;'password'&#93;;
			
			if(empty($username) OR empty($password)){
				echo "Fill in all the fields";
				exit();
			}

			//check remeber me button is set or not
			if(isset($_POST&#91;'re'&#93;)){
				$re = "on";
			}else{
				$re = "";
			}

			//check username and pass
			$query = mysqli_query($con,"SELECT * FROM `user` WHERE username='$username' AND password='$password'");

			if(mysqli_num_rows($query) == 1){

				//login the user in
				if($re == "on"){ //remember me checked
					setcookie("username",$username,time() + (86400  * 10));
				}else{ //remember me not checked
					session_start();
					$_SESSION&#91;'username'&#93; = $username;
				}

				echo "user logedin";
				header("Location: index.php");
				exit();
			}

			echo "Invalid username and password";
			exit();

		}
	?>
</body>
</html>

index.php

<?php
	session_start();
	//when user is logged in with session
	if (isset($_SESSION&#91;'username'&#93;)) {
		echo $_SESSION&#91;'username'&#93;  . " Session <a href='logout.php'>logout</a>";
	}else if(isset($_COOKIE['username'])){ //when user is logged with cookies
		echo $_COOKIE['username']  . " cookie <a href='logout.php'>logout</a>";
	}else{ //when user is not loggedin
		header("Location: login.php");
		exit();
	}
?>

logout.php

<?php
 session_start();

 session_destroy();

 setcookie("username","", time() - (86400  * 10));

 header("Location: login.php");
 exit();
?>