Tuesday 5 April 2016

PHP Login Code with Remember me Login Details



In this login code, I have used remember me features to add login details like username or email and password which are introduced by user itself. So, When user come for login again on the same login page then at that time this details will be populated in the login form. So, This type of remember login details features will help to user in reducing user attempt and stop to type login details for every time again and again.

In my previous post, I have already describe how to make login page in PHP along with Session variable. But In this post I am going to display you a lesson regarding how to make login page in PHP along with remember me functionality by using $_COOKIE variable array. In this tutorial I have applying PHP cookies for store user details like username or email and password.

First of all What is the features of Remember Me in PHP Login page and why we required it in our web application? Mainly when user login to our web application and suppose he check the remember me checkbox tag which is placed below password textbox and after this the user will be login into our web application or website then after he will close the browser or shutdown his pc, he cannot be logout to system, he can only logout by clicking on logout button. And when he again come for login with same browser then his details will be automatically filled in login form. So this is mail use of Remember Me functionality in PHP Login Form.





Source Code


index.php


 
  <?php  
session_start();
if(isset($_SESSION["admin_name"]))
{
 header("location:home.php");
}
$connect = mysqli_connect("localhost", "root", "", "testing");  
if(isset($_POST["login"]))   
{  
 if(!empty($_POST["member_name"]) && !empty($_POST["member_password"]))
 {
  $name = mysqli_real_escape_string($connect, $_POST["member_name"]);
  $password = md5(mysqli_real_escape_string($connect, $_POST["member_password"]));
  $sql = "Select * from admin_login where admin_name = '" . $name . "' and admin_password = '" . $password . "'";  
  $result = mysqli_query($connect,$sql);  
  $user = mysqli_fetch_array($result);  
  if($user)   
  {  
   if(!empty($_POST["remember"]))   
   {  
    setcookie ("member_login",$name,time()+ (10 * 365 * 24 * 60 * 60));  
    setcookie ("member_password",$password,time()+ (10 * 365 * 24 * 60 * 60));
    $_SESSION["admin_name"] = $name;
   }  
   else  
   {  
    if(isset($_COOKIE["member_login"]))   
    {  
     setcookie ("member_login","");  
    }  
    if(isset($_COOKIE["member_password"]))   
    {  
     setcookie ("member_password","");  
    }  
   }  
   header("location:home.php"); 
  }  
  else  
  {  
   $message = "Invalid Login";  
  } 
 }
 else
 {
  $message = "Both are Required Fields";
 }
}  
 ?>  
<html>  
 <head>  
  <title>Webslesson - Tutorial</title>  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  <style>  
  body  
  {  
   margin:0;  
   padding:0;  
   background-color:#f1f1f1;  
  }  
        .box  
        {  
   width:700px;  
   padding:20px;  
   background-color:#fff;  
  }  
  </style>  
 </head>  
 <body>  
  <div class="container box">  
   <form action="" method="post" id="frmLogin"> 
    <h3 align="center">PHP Login script with Remember me Login Details</h3><br />
    <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>
 

home.php


 
  <?php  
session_start();  
if(!isset($_SESSION["admin_name"]))
{
 header("location:index.php");
}
?>  
<html>  
 <head>  
  <title>Webslesson - Tutorial</title>  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  <style>  
  body  
  {  
   margin:0;  
   padding:0;  
   background-color:#f1f1f1;  
  }  
        .box  
        {  
   width:500px;  
   padding:20px;  
   background-color:#fff;  
  }  
  </style>  
 </head>  
 <body>  
  <div class="container box">  
   <h3 align="center">Welcome - <?php echo $_SESSION["admin_name"]; ?></h3>
   <br />
   <p><a href="logout.php">Logout</a></p>
  </div>  
 </body>  
</html>
 

logout.php


 
<?php
session_start();
unset($_SESSION["admin_name"]);
header("location:index.php");
?>
 

Database


 
 --
-- Table structure for table `admin_login`
--

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', '5f4dcc3b5aa765d61d8327deb882cf99');
 

10 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
  3. Hello........
    when you are to logout you have to unset the session[user_name]

    ReplyDelete
  4. i can't login
    my account and password are admin and admin. but It invalid login!

    ReplyDelete
  5. i can't login
    my account and password are admin and admin. but It invalid login!

    ReplyDelete
  6. Hey thanks man, but it says invalid login

    What can I do?

    ReplyDelete
  7. If you can't login the system, you have to Change your DB admin_password field data to 21232f297a57a5a743894a0e4a801fc3 then, enter Username = admin Password = admin.

    ReplyDelete