PHP Login Script with Remember Me

Last modified on June 25th, 2018 by Vincy.
This post is outdated. See Secure Remember Me for Login using PHP Session and Cookies for creating a login with Remember Me.

In a login script, remember me feature is used to preserve the login name and password entered by the user. And it can be populated in the login form at the time of login. It minimizes the user effort by preventing to enter login details for each time.

Some days before, we have seen PHP login script with session. Now we are going to see an example for login script with remember me feature. In this example, we are using PHP cookies for preserving user login and password.


PHP Login Form

This code shows login form with the PHP code to pre-populate user login and password. This form has a remember me check box. If it is set then the login details entered by the user will be preserved for future login attempts.

<form action="" method="post" id="frmLogin">
	<div class="error-message"><?php if(isset($message)) { echo $message; } ?></div>	
	<div class="field-group">
		<div><label for="login">Username</label></div>
		<div><input name="member_name" type="text" value="<?php if(isset($_COOKIE["member_login"])) { echo $_COOKIE["member_login"]; } ?>" class="input-field">
	<div class="field-group">
		<div><label for="password">Password</label></div>
		<div><input name="member_password" type="password" value="<?php if(isset($_COOKIE["member_password"])) { echo $_COOKIE["member_password"]; } ?>" class="input-field"> 
	<div class="field-group">
		<div><input type="checkbox" name="remember" id="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?> />
		<label for="remember-me">Remember me</label>
	<div class="field-group">
		<div><input type="submit" name="login" value="Login" class="form-submit-button"></span></div>

PHP Cookie to Remember Login

This PHP code validates the login details entered by the user while submitting the form. And it checks whether the remember me is checked. If so, it stores the user login and password in PHP $_COOKIE array. If these array variables are not empty then it will be populated in the login form fields.

if(!empty($_POST["login"])) {
	$conn = mysqli_connect("localhost", "root", "", "blog_samples");
	$sql = "Select * from members where member_name = '" . $_POST["member_name"] . "' and member_password = '" . md5($_POST["member_password"]) . "'";
	$result = mysqli_query($conn,$sql);
	$user = mysqli_fetch_array($result);
	if($user) {
			$_SESSION["member_id"]		   = $user["member_id"];
			if(!empty($_POST["remember"])) {
				setcookie ("member_login",$_POST["member_name"],time()+ (10 * 365 * 24 * 60 * 60));
				setcookie ("member_password",$_POST["member_password"],time()+ (10 * 365 * 24 * 60 * 60));
			} else {
				if(isset($_COOKIE["member_login"])) {
					setcookie ("member_login","");
				if(isset($_COOKIE["member_password"])) {
					setcookie ("member_password","");
	} else {
		$message = "Invalid Login";


This PHP code tutorial was published on March 23, 2016.

↑ Back to Top

Share this Article