PHP Login Script with Remember Me

Last modified on December 11th, 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 details 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-script-with-remember-me

PHP Login Form

This code shows the login form with the PHP code to pre-populate user login details. This form has a remember me checkbox. 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>
	<div class="field-group">
		<div><label for="password">Password</label></div>
		<div><input name="member_password" type="password" value="" class="input-field"> 
	</div>
	<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>
	<div class="field-group">
		<div><input type="submit" name="login" value="Login" class="form-submit-button"></span></div>
	</div>       
</form>

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.

<?php
session_start();
if(!empty($_POST["login"])) {
	$conn = mysqli_connect("localhost", "root", "", "blog_samples");
	$sql = "Select * from members where member_name = '" . $_POST["member_name"] . "'";
        if(!isset($_COOKIE["member_login"])) {
            $sql .= " 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));
			} else {
				if(isset($_COOKIE["member_login"])) {
					setcookie ("member_login","");
				}
			}
	} else {
		$message = "Invalid Login";
	}
}
?>
This post is outdated. See Secure Remember Me for Login using PHP Session and Cookies for creating a login with Remember Me.

↑ Back to Top

Share this Article