Tuesday, 5 April 2016

PHP Login Code with Remember me Login Details



In this login code, I have used remember me functionality to save login details like username or email and password introduced by user. When user come for login then at that time this details can be populated in the login form. So, It will reduces user attempt and stop to type login details for every time. Before a few days back, I have show PHP login Code along with session. But Today I am going to show you a lesson for login code along with remember me functionality. In this tutorial I have applying PHP cookies for store user details like username or email and password.

Source Code

Database

 CREATE TABLE IF NOT EXISTS `admin_login` (  
  `admin_id` int(11) NOT NULL AUTO_INCREMENT,  
  `admin_name` varchar(250) NOT NULL,  
  `admin_password` varchar(250) NOT NULL,  
  PRIMARY KEY (`admin_id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;  
 --  
 -- Dumping data for table `admin_login`  
 --  
 INSERT INTO `admin_login` (`admin_id`, `admin_name`, `admin_password`) VALUES  
 (1, 'admin', 'admin');  

login.php

 <?php  
 session_start();  
 $conn = mysqli_connect("localhost", "root", "", "testing");  
 if(isset($_POST["login"]))   
 {  
      $sql = "Select * from admin_login where admin_name = '" . $_POST["member_name"] . "' and admin_password = '" . $_POST["member_password"] . "'";  
      $result = mysqli_query($conn,$sql);  
      $user = mysqli_fetch_array($result);  
      if($user)   
      {  
           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","");  
                }  
           }  
           header("location:index.php");  
      }  
      else  
      {  
           $message = "Invalid Login";  
      }  
 }  
 ?>  
 <html>  
      <head>  
           <title>Webslesson - Tutorial</title>  
           <script src="jquery.js"></script>  
           <script src="js/bootstrap.js"></script>  
           <link href="css/bootstrap.css" rel="stylesheet" />  
           <style>  
                body  
                {  
                     margin:0;  
                     padding:0;  
                     background-color:#f1f1f1;  
                }  
                .box  
                {  
                     width:500px;  
                     padding:20px;  
                     background-color:#fff;  
                }  
           </style>  
      </head>  
      <body>  
           <div class="container box">  
                <form action="" method="post" id="frmLogin">  
                     <div class="text-danger"><?php if(isset($message)) { echo $message; } ?></div>  
                     <div class="form-group">  
                          <label for="login">Username</label>  
                          <input name="member_name" type="text" value="<?php if(isset($_COOKIE["member_login"])) { echo $_COOKIE["member_login"]; } ?>" class="form-control" />  
                     </div>  
                     <div class="form-group">  
                          <label for="password">Password</label>  
                          <input name="member_password" type="password" value="<?php if(isset($_COOKIE["member_password"])) { echo $_COOKIE["member_password"]; } ?>" class="form-control" />   
                     </div>  
                     <div class="form-group">  
                          <input type="checkbox" name="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?> />  
                          <label for="remember-me">Remember me</label>  
                     </div>  
                     <div class="form-group">  
                          <div><input type="submit" name="login" value="Login" class="btn btn-success"></span></div>  
                     </div>   
                </form>  
                <br />  
           </div>  
      </body>  
 </html>  

2 comments:

  1. Hi, nice tutorial.

    When you logout do you need to unset (clean) any variables?

    ReplyDelete
  2. Hi, nice tutorial.

    When you logout do you need to unset (clean) any variables?

    ReplyDelete