PHPWeb Development

PHP login with remember me function

Remember me function is used to save the username and password in login form  entered by the user. In this tutorial we are using COOKIES for saving preserving username and user password in the login from.
Step 1 :  Create a simple html form

<div align="center">
<form action="" method="post" id="login">
<div class="error-message"><?php if(isset($message)) { echo $message; } ?></div>
<h4> PHP login with remember me function</h4>
<div class="field-group">
<div><label for="login">Username</label></div>
<div>
<input name="username" type="text" value="<?php if(isset($_COOKIE["user_login"])) { echo $_COOKIE["user_login"]; } ?>" class="input-field">
</div>
<div class="field-group">
		<div><label for="password">Password</label></div>
		<div><input name="password" type="password" value="<?php if(isset($_COOKIE["userpassword"])) { echo $_COOKIE["userpassword"]; } ?>" class="input-field">
	</div>
	<div class="field-group">
		<div><input type="checkbox" name="remember" id="remember" <?php if(isset($_COOKIE["user_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>
</div>

Step 2 : Create a SQL table tbluser

CREATE TABLE `tbluser` (
  `id` int(11) NOT NULL,
  `userName` varchar(200) DEFAULT NULL,
  `userPassword` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tbluser`
  ADD PRIMARY KEY (`id`);

Step 3 : Create database configuration file config.php

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'test'); // your database name
$conn = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

Step 4 : PHP Cookies to remember username and password. Username and password will be stored $_COOKIE array.

<?php
session_start(); // starting a session
include('config.php'); // Database configuration file
if(isset($_POST["login"]))
{
// Getting Post Vlaues
$username=$_POST['username'];
$password=md5($_POST['password']); // MD5 password encryption
$sql = "Select * from tbluser where userName ='$username' and userPassword ='$password'";
	$result = mysqli_query($conn,$sql);
	$row = mysqli_fetch_array($result);
	if($row) {
			$_SESSION["userid"]= $row["id"];
	// if remember me clicked . Values will be stored in $_COOKIE  array
			if(!empty($_POST["remember"])) {
//COOKIES for username
setcookie ("user_login",$_POST["username"],time()+ (10 * 365 * 24 * 60 * 60));
//COOKIES for password
setcookie ("userpassword",$_POST["password"],time()+ (10 * 365 * 24 * 60 * 60));
} else {
if(isset($_COOKIE["user_login"])) {
setcookie ("user_login","");
if(isset($_COOKIE["userpassword"])) {
setcookie ("userpassword","");
				}
			}
	header('location:welcome.php');
	} else {
		$message = "Invalid Login";
	}
}
?>

View Demo———————————————–
Username  : anuj@gmail.com
Password : Test@123

PHP login with remember me function(Download Source Code)
Size: 3.45 KB
Version: V1.0

How to run this script
1. Download and Unzip file on your local system.
2. Put this file inside root directory
3. Database Configuration
Database Configuration
Open phpmyadmin
Create Database test
Import database tbluser.sql (available inside zip package)
put this url in the browser http://localhost/loginscript-with-rememberme-function
Login detail for demo
Username  : anuj@gmail.com
Password : Test@123

Recommended Tutorials for you


Tags

Anuj kumar

This is Anuj Kumar. I’m a professional web developer with 5+ years of experience. I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials. .

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button
Close
Close