Jump to content

Sessions and cookies in login


graham23s

Recommended Posts

Hi Guys,

 

My login system uses sessions toi store the users id and username (Just use id for getting info from mysql etc), i'm trying to figure out, if they tick the rememeber me button on the login form, how i remember them lol i set the cookie fine but when testing the page is never remembered:

 

code:

 

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

   // CHECK TO SEE IF THE REMEMBER ME WAS TICKED
   if (isset($_COOKIE['customers_cookie_id']))
   {
           // VARS
           $varCookie = $_COOKIE['customers_cookie_id'];
   }

           // VARS 
           $var_loggedinuserid = $_SESSION['id'];
    $var_loggedinuserfirstname = $_SESSION['first_name'];
?>

 

thats's the code thats at the top of my protected pages! if i echo out $varCookie i get 6 which is my ID!

 

so i...

 

1) tick the remember me button

2) set a cookie if its ticked (with the users id)

 

then i'm stuped lol if im using sessions how could i use cookies kinda thing

 

any advice would be great

 

cheers

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/135417-sessions-and-cookies-in-login/
Share on other sites

You want to check the cookie before the sessions are checked. Store for example the user id in the cookie, then when they come back to the site check for the cookie, if the cookie is set run a login script that uses that id. That will login the user and make the session check return true

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.