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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<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
1 2 3 4 5 6 7 |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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 KBVersion: 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