Jump to content

Login and remember me


graham23s

Recommended Posts

Hi Guys,

 

on my site i use sessions for the login! but i have put a "rememeber me next time i log in" checkbox on the login page:

 

login.php

 

<?php
ob_start();
session_start(); 
include("inc/inc-dbconnection.php");
include("inc/inc-online.php");
include("inc/inc-header.php");
include("inc/inc-navigation.php");
?>
<?php
// standard header //
print("<div class=\"subheader\"><div id=\"title\">Home > <span class=\"blue\">Login to your account</span></div>You can update your personal details and view orders in here. forgot your password? no worries recover it <a class=\"smart_links\" href=\"recover-password.php\">here</a>.</div>");

// check to see if the session exists //
if(isset($_SESSION['id']))
{
print("<div id=\"shopping_login_error\">You are already logged in.</div>");
include("inc/inc-footer.php");
exit;
}

// login box //
print('<form action="login.php" method="POST" onSubmit="return login_check()" name="login_form" />');

print("<table border=\"0\" cellpadding=\"5\" cellspacing=\"1\" class=\"tbl_login\">\n");
print("<tr>\n");
print("<td colspan=\"2\" align=\"left\" class=\"c3\"><b>Login</b></td>\n");
print("</tr>\n");
print("<tr class=\"c5\">\n");
print("<td align=\"left\"><img src=\"images/pixel.gif\" width=\"1\" height=\"1\"></td><td align=\"left\"><img src=\"images/pixel.gif\" width=\"1\" height=\"1\"></td>\n");
print("</tr>\n");

// check for the isset
if (isset($_POST['submit_login']))
{

  // INITIALISE ERROR ARRAY
  $errors = array();
  
  // VARS
  $email    = mysql_real_escape_string(trim($_POST['email']));
  $password = mysql_real_escape_string(trim($_POST['password']));
  $cookie   = $_POST['remember'];

   // EMPTY FIELDS CHECK INCASE JAVASCRIPT IS DISABLED
   if(empty($email) || empty($password))
   {
    $errors[] = "You never filled in both login fields.";
   }

    // VALIDATE EMAIL ADDRESS
    if (!(ereg ("^.+@.+\..+$", $email))) 
    { 
     $errors[] = "Your e-mail address looks invalid.";
    }

     // CHECK IF THE LOGIN DETAILS ARE IN THE DATABASE
     $q_login = "SELECT `id`,`email`,`password`,`first_name` FROM `fcp_customers` WHERE `email`='$email' AND `password`='$password' LIMIT 1";
     $r_login = mysql_query($q_login) or die (mysql_error());
     $row = mysql_fetch_array($r_login);

      // RESULTS
      $num = mysql_num_rows($r_login);

       if($num != 1)
       { 
        $errors[] = "The details you have submitted don't appear to be in our database."; 
       } 

        // IF THERE ARE ANY ERRORS THEY WILL BE IN THE ARRAY! COUNT HOW MANY
        if (count($errors) > 0)
        {
    
        // print hidden tr
        print("<tr>\n");
        print("<td colspan=\"2\" align=\"left\">\n");
    
        print("<ul class=\"bullets\">");
        
        foreach($errors as $error)
        {
        print("<li><img src=\"images/topbullet.gif\"> $error"); 
        }                 
        print("</ul>");  
      
        // end tr
        print("</tr>\n");
        print("</td>\n");
   
        } else {

        // there was results so update the login timer and set a session 
        $querytimer = mysql_query("UPDATE `fcp_customers` SET `last_logged_in`=NOW() WHERE `email`='$email' AND `password`='$password'");
  
        // sessions...
        $_SESSION['id']         = $row['id'];
        $_SESSION['first_name'] = $row['first_name'];
  
        // validation...
        $_SESSION['logged_in']  = 'yes';   
  
        ##################################################################################################
        # ONLY EXECUTE IF THE REMEMBER ME IS CHECKED
        ##################################################################################################
        if (isset($cookie))
        {
         // if it is set the set a cookie 
         // cookies...
         $_COOKIE['id']          = $row['id'];
         $_COOKIE['first_name']  = $row['first_name'];
  
          // if it is set the set a cookie 
          setcookie("customers_cookie_id", $_SESSION['id'], time()+3600);
          setcookie("customers_cookie_customer", $_SESSION['first_name'], time()+3600);

        }
        ##################################################################################################
        # ONLY EXECUTE IF THE REMEMBER ME IS CHECKED
        ##################################################################################################
  
        // LASTLY REDIRECT TO THE ACCOUNT PAGE
        header("Location: account.php"); 
        ob_clean();  
        //exit;
  
    } 

} // END THE ISSET

print("<tr class=\"c1\">\n");
print("<td align=\"left\" class=\"font_for_forms\">E-Mail:</td><td class=\"class=\"font_for_forms\" align=left><input type=\"text\" size=\"40\" name=\"email\" /></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td align=\"left\" class=\"font_for_forms\">Password:</td><td class=\"class=\"font_for_forms\" align=left><input type=\"password\" size=\"40\" name=\"password\" /></td>\n");
print("</tr>\n");
print("<tr class=\"c1\">\n");
print("<td align=\"right\" class=\"font_for_forms\"> </td><td class=\"font_for_forms\" align=\"left\"><input type=\"checkbox\" name=\"remember\" value='yes'>Remember me next time i log in.</td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td colspan=\"2\" align=\"right\" class=\"font_for_forms\"><input type=\"submit\" name=\"submit_login\" value=\"Login\"></td>");
print("</tr>\n");
print("</table>\n");

print("</form>\n");
print("<br />");

// just a little note to remember them!
print("<span class=\"msg_remember\">You must be logged in to place orders. you can register an account <a class=\"smart_links\" href=\"register.php\">here</a>.</span>");

?>
<?php
// include the footer //
include("inc/inc-footer.php");
?>

 

inc-sessions.php

 

<?php
ob_start();
session_start(); 
  if($_SESSION['logged_in'] != 'yes') { 
    header("Location: login.php"); 
} 

// check to see if a cookie is set...
if (isset($_COOKIE['customers_cookie_id']))
{
// vars
$c_id = $_COOKIE['id'];
$c_nm = $_COOKIE['first_name'];
}

// a variable for easier access //
       $var_loggedinuserid = $_SESSION['id'];
$var_loggedinuserfirstname = $_SESSION['first_name'];
?>

 

the code the way i have it doesn't remember you! i'm not sure what i need to add for it to do so!

 

is there anything obvious im missing?

 

thanks a lot guys

 

Graham

 

 

Link to comment
https://forums.phpfreaks.com/topic/133715-login-and-remember-me/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.