Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put remember me (cookie) in PHP

Tags:

php

cookies

I was making a login page. so I already can login into another page. then in my login page I need to put remember me checkbox and PHP. so which part in this codes that I need to put my "remember me " codes ? please help me.

this is login1.php

<?php
session_start();
//database connection
$servername     = "localhost";
$username       = "root";
$password       = "";
$dbname         = "lala";

// Create connection
$link = mysql_connect($servername,$username,$password) or die("Could not connect");

$db= mysql_select_db("$dbname",$link) or die ("Could not select database");

$login = $_POST['login'];
$password = md5($_POST['password']);
$rememberme = $_POST['remember_me'];

$result = mysql_query("SELECT * from admin WHERE working_id = '$login' and password = '$password'");
$count = mysql_num_rows($result);

if($count==1)
{
    //check remember me is on or off
    //if off then session login
    //else add cookie

    $_SESSION['username'] = $login;
    $_SESSION['password'] = $password;

    $result1 = mysql_query("SELECT * from admin WHERE working_id = '$login' and password = '$password'");
    while($row = mysql_fetch_array($result1)){
        $_SESSION['gp'] = $row['gpType'];
    }
    header('Location:dashboard.php');
}
else 
{
    $_SESSION['username'] = NULL;
    $_SESSION['password'] = NULL;

    ?>
    <script type = "text/Javascript">
    alert("Sorry , wrong username or password");
    setTimeout("location.href = 'abc.php';");
    </script>
    <?php
}
?>

this is my html

    <p><input type="password" name="password" value="" placeholder="Password"></p>
    </div>
     <div id="form2">

    <p class="remember_me">
      <label>

        <input type="checkbox" name="remember_me" id="remember_me">
        Remember me 
      </label>
    </p></div>
    <div id="form3">
    <p class="submit"><input type="submit" name="commit" value="Login"></p>
  </form>
  </div>
like image 686
ace Avatar asked Dec 16 '14 07:12

ace


People also ask

How add remember me in PHP?

Form submitCheck if the username and password exist in the users table or not. If exists then assign user id to $userid variable. If 'rememberme' is POST then encrypt the userid and set 'rememberme' COOKIE for 30 days. Assign $userid to $_SESSION['userid'] and redirect to home.

How do you use Remember me cookies?

Clicking the “Remember Me” box tells the browser to save a cookie so that if you close out the window for the site without signing out, the next time you go back, you will be signed back in automatically.

How do I add remember me to my login page?

Just before the body closing tag (</body>) add the following line of code. In the login form, we will have two input fields (username and password), one checkbox (remember me) and a submit button.

How do I make PHP Keep me logged in?

Hence the user can log in without having to enter the Username and Password again until the life of that cookie expires. The example code given below is the way how to remember password checkbox works through PHP. $name = mysqli_real_escape_string( $connect , $_POST [ "user_name" ]);


1 Answers

Just Use this code after getting the $login and $password

<?php   
if($_POST["remember_me"]=='1' || $_POST["remember_me"]=='on')
                    {
                    $hour = time() + 3600 * 24 * 30;
                    setcookie('username', $login, $hour);
                         setcookie('password', $password, $hour);
                    }
?>
like image 139
khan Asim Avatar answered Nov 12 '22 17:11

khan Asim