Jump to content

How to add "remember me" feature


xgd

Recommended Posts

Hello,

 

I have made this registration script with all th features i wanted except "remembre me" feature, which i want to add to the login page but dont know how.

 

I am using sessions, i know about the time() parameter for cookies but cannot squezze it all in to get what i want, so can someone help me out ?

 

So, just a checkbox that if checked keeps the user logged in forever.

 

this is my login script:

 

<?php

     session_start();


   if (isset($_POST['submit'])) {
   
       require_once('db_connect.php');
   
if (empty($_POST['email'])) {
   
    echo "didnt enter EMAKL<br />";
	} else {
	$e = trim($_POST['email']);
	}

if (empty($_POST['pass'])) {

   echo "Didnt enter PASS<br />";
   }
   else {
   $p = trim($_POST['pass']);

       }
   
   if ($e && $p) {
   
    $q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')";

	$r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1");

	if (mysqli_num_rows($r) == 1) {

	   $_SESSION = mysqli_fetch_array($r);
	   
	   header("Location: index.php");
	   exit();
	   }
	   else {
	   echo "Youre not in hte DB<br />";
	   }
	   }
	   }
	   ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style2 {color: #333333}
.style3 {color: #ECE9D8}
-->
</style>
</head>

<body>
<p><a href="register.php">Register</a>     <a href="login.php">Login</a>    <a href="password.php">Change Password</a>     <a href="view_users.php">View Users</a> <a href="private.php">private</a></p>

<h1>Login</h1>
<form action="login.php" method="post">
<input type="text" name="email" />
<input type="text" name="pass" />
<input type="submit" name="submit" value="Login" />
</form>


</body>
</html>

 

Thanks in advance.

Link to comment
Share on other sites


<?php

     session_start();


   if (isset($_POST['submit'])) {
   
       require_once('db_connect.php');
   
if (empty($_POST['email'])) {
   
    echo "didnt enter EMAKL<br />";
	} else {
	$e = trim($_POST['email']);
	}

if (empty($_POST['pass'])) {

   echo "Didnt enter PASS<br />";
   }
   else {
   $p = trim($_POST['pass']);

       }
   
   if ($e && $p) {
   
    $q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')";

	$r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1");

	if (mysqli_num_rows($r) == 1) {

	   $_SESSION = mysqli_fetch_array($r);
	   
	   header("Location: index.php");
	   exit();
	   }
	   //i HERE IS THE CHANGE
	   if(isset($_POST['remember'])){
        setcookie("cookid", $_SESSION['user_id'], time()+60*60*24*100);
        setcookie("cookname", $_SESSION['first_name'], time()+60*60*24*100);
	setcookie("cooklevel", $_SESSION['user_level'], time()+60*60*24*100);
          }

	   
	     //THE CHANGE ENDS HERE 
	   else {
	   echo "Youre not in hte DB<br />";
	   }
	   }
	   }
	   ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style2 {color: #333333}
.style3 {color: #ECE9D8}
-->
</style>
</head>

<body>
<p><a href="register.php">Register</a>     <a href="login.php">Login</a>    <a href="password.php">Change Password</a>     <a href="view_users.php">View Users</a> <a href="private.php">private</a></p>

<h1>Login</h1>
<form action="login.php" method="post">
<input type="text" name="email" />
<input type="text" name="pass" />
<input type="submit" name="submit" value="Login" />
<label>
<input type="checkbox" name="remember" id="remember" />
Remember me:</label>
</form>


</body>
</html>


i put what you told me in the same place but it didnt work out.

 

Can you modify the script so that it contains the checkbox and the codethat always has the user logged in if checked, and post the WHOLE script ?

 

Thanks

Link to comment
Share on other sites

I also tried to modify that part by puttring the cookies before such as:

 

$q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')";

 

$r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1");

 

if (mysqli_num_rows($r) == 1) {

 

  $_SESSION = mysqli_fetch_array($r);

  if(isset($_POST['remember'])){

        setcookie("cookid", $_SESSION['user_id'], time()+60*60*24*100);

        setcookie("cookname", $_SESSION['first_name'], time()+60*60*24*100);

setcookie("cooklevel", $_SESSION['user_level'], time()+60*60*24*100);

         

 

  header("Location: index.php");

  exit();

  }

  }

 

 

    //IT ENDS 

  else {

 

but this didnt work out either

Link to comment
Share on other sites

<?php

if (isset($_POST['submit'])) {
    require_once('db_connect.php');
    if (empty($_POST['email'])) {
        echo "didnt enter EMAKL<br />";
    } else {
        $e = trim($_POST['email']);
    }

    if (empty($_POST['pass'])) {
        echo "Didnt enter PASS<br />";
    }
    else {
        $p = trim($_POST['pass']);

    }

    if ($e && $p) {
        $q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')";
        $r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1");
        if (mysqli_num_rows($r) == 1) {
            if(isset($_POST['remember'])) {
                session_set_cookie_params(86400);
            }
            session_start();
            $_SESSION = mysqli_fetch_array($r);
            setcookie("cookid", $_SESSION['user_id'], time()+60*60*24*100);
            setcookie("cookname", $_SESSION['first_name'], time()+60*60*24*100);
            setcookie("cooklevel", $_SESSION['user_level'], time()+60*60*24*100);
            header("Location: index.php");
            exit();
        }
        else {
            echo "Youre not in hte DB<br />";
        }
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <style type="text/css">
            <!--
            .style2 {color: #333333}
            .style3 {color: #ECE9D8}
            -->
        </style>
    </head>

    <body>
        <p><a href="register.php">Register</a>     <a href="login.php">Login</a>    <a href="password.php">Change Password</a>     <a href="view_users.php">View Users</a> <a href="private.php">private</a></p>

        <h1>Login</h1>
        <form action="login.php" method="post">
            <input type="text" name="email" />
            <input type="text" name="pass" />
            <input type="submit" name="submit" value="Login" />
            <label>
                <input type="checkbox" name="remember" id="remember" />
                Remember me:</label>
        </form>


    </body>
</html>

 

I also took the liberty to format your code to something more readable.

Link to comment
Share on other sites

Hey man,

 

Thanks for modifying.

 

I still dont see it working, i tested it, i get logged in, but when i close the browser, reopen it and go to index.php, i am not logged in.

 

At the index.php page, i use this to check if the user is logged in (and it works, but i am not sure if i should modify that also because of the remember me feature:

 

<?php
session_start();

if (isset($_SESSION['first_name'])) {
  
   echo "Hello man<br />";
   
   echo "<a href=\"logout.php\">logout</a>";
   
   }
   
   
   
   ?>

 

 

Link to comment
Share on other sites

I still dont see it working, i tested it, i get logged in, but when i close the browser, reopen it and go to index.php, i am not logged in.

 

Read the contents of your cookie and check for how long it is set. Or ouput the time using session_get_cookie_params() and if ttl is still 0 then session_destroy() when you login:

session_start(); 
if (sizeof($_SESSION)) {
    session_destroy();
}
..

Link to comment
Share on other sites

This just confuses me further.

 

To put it simply, are you bothered (or anyone else) to rewrite my original login.php script so that it contains an option for a user to always stay logged in and also if need be modify the way i check to see if the user is logged (because of the remember me feature) ? I believe it will not take more then a few minutes.

 

Thanks for trying to help, but i am not that good with php to understand what youre pointing at.

Link to comment
Share on other sites

How is this posted in the wrong section ? Should i put it in the freelance section ?

 

I am not asking for someone to write an entire app for me, just help me modify this script i have already made, but  cant successfully modify.

 

Can someone help me with this ?

Link to comment
Share on other sites

Try this:

 

<?php

     session_start();


   if (isset($_POST['submit'])) {
   
       require_once('db_connect.php');
   
if (empty($_POST['email'])) {
   
    echo "didnt enter EMAKL<br />";
	} else {
	$e = trim($_POST['email']);
	}

if (empty($_POST['pass'])) {

   echo "Didnt enter PASS<br />";
   }
   else {
   $p = trim($_POST['pass']);

       }
   
   if ($e && $p) {
   
    $q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')";

	$r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1");

	if (mysqli_num_rows($r) == 1) {

	   $_SESSION = mysqli_fetch_array($r);
	   //i HERE IS THE CHANGE
	   if(isset($_POST['remember'])){
        setcookie("cookid", $_SESSION['user_id'], time()+60*60*24*100);
        setcookie("cookname", $_SESSION['first_name'], time()+60*60*24*100);
	setcookie("cooklevel", $_SESSION['user_level'], time()+60*60*24*100);
          }
	   
	   header("Location: index.php");
	   exit();
	   }		   
	     //THE CHANGE ENDS HERE 
	   else {
	   echo "Youre not in hte DB<br />";
	   }
	   }
	   }
	   ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style2 {color: #333333}
.style3 {color: #ECE9D8}
-->
</style>
</head>

<body>
<p><a href="register.php">Register</a>     <a href="login.php">Login</a>    <a href="password.php">Change Password</a>     <a href="view_users.php">View Users</a> <a href="private.php">private</a></p>

<h1>Login</h1>
<form action="login.php" method="post">
<input type="text" name="email" />
<input type="text" name="pass" />
<input type="submit" name="submit" value="Login" />
<label>
<input type="checkbox" name="remember" id="remember" />
Remember me:</label>
</form>


</body>
</html>

Link to comment
Share on other sites

I do try to understand the ode of course, and i will understand it if i ever get it working, i just cant make it work all the way.

 

Mattal, what exactly did you change in the code ?

 

also will someone reply to my question to which i believe also is a very big issue, do i need to change the way i check if the user is logged in if i want to use the remember me feature ?

 

Currently i check it like this (and it works, but without the remember me feature):

 

if (!isset($_SESSION('first_name'])) {

 

  header("Location: blabla.php");

exit();

}

 

 

 

Link to comment
Share on other sites

do i need to change the way i check if the user is logged in if i want to use the remember me feature ?

 

Currently i check it like this (and it works, but without the remember me feature):

 

if (!isset($_SESSION('first_name'])) {

 

  header("Location: blabla.php");

exit();

}

 

Link to comment
Share on other sites

do i need to change the way i check if the user is logged in if i want to use the remember me feature ?

 

Currently i check it like this (and it works, but without the remember me feature):

 

if (!isset($_SESSION('first_name'])) {

 

  header("Location: blabla.php");

exit();

}

 

 

Yeah that will still work.

Link to comment
Share on other sites

I do try to understand the ode of course, and i will understand it if i ever get it working, i just cant make it work all the way.

 

Mattal, what exactly did you change in the code ?

 

also will someone reply to my question to which i believe also is a very big issue, do i need to change the way i check if the user is logged in if i want to use the remember me feature ?

 

Currently i check it like this (and it works, but without the remember me feature):

 

if (!isset($_SESSION('first_name'])) {

 

  header("Location: blabla.php");

exit();

}

 

I moved the section of the code which checks for the login and sets the cookies, because you had it in the wrong place (you had it after the } else {, not before it).

 

Did it work though?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.