Advertisement
tamaro_skaljic

remember_me.php

Jun 27th, 2021
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.06 KB | None | 0 0
  1. <?php
  2. /*
  3.  * PHP session (cookies) exist only until the device is shut down. In order for users to "stay" logged in after that,
  4.  * another cookie is needed, which is stored beyond the the device shut down. The following code checks whether
  5.  * this cookie exists and whether the user has shut down the device in the meantime. If so, it checks whether the cookie
  6.  * value is associated with a user and if so, a new session cookie is created.
  7.  *
  8.  * TODO: The current implementation allows this functionality only for a maximum of one browser instance. If the user
  9.  * logs in with another browser instance and shut down the device, the user is no longer logged in when
  10.  * reopening the first browser instance and must log in again.
  11.  */
  12.  
  13. // If the user isn't logged in but a session cookie exist
  14. if(empty($_SESSION['user_id']) and isset($_COOKIE['session_id'])) {
  15.     // Check whether the cookie value is associated with a user
  16.     $query = "SELECT user_id, is_admin FROM public.user WHERE session_id = :session_id";
  17.     $query_params = array(':session_id' => $_COOKIE['session_id']);
  18.     try{
  19.         $stmt = $pdo->prepare($query);
  20.         $result = $stmt->execute($query_params);
  21.     }catch(PDOException $ex){
  22.         echo '<span style="color: red">ERROR! Code: 002</span>';
  23.         exit;
  24.     }
  25.     $row = $stmt->fetch();
  26.  
  27.     if(empty($row)){
  28.         // If not, remove the session cookie. It's an old persisted session ID and is no longer needed.
  29.         unset($_COOKIE['session_id']);
  30.         setcookie('session_id', "", time() - 1, '/chat/');
  31.     } else {
  32.         // If true, log the user in and
  33.         session_start();
  34.         $_SESSION['user_id'] = $row['user_id'];
  35.         $_SESSION['is_admin'] = $row['is_admin'];
  36.         // create a new persisted session id and cookie. This is to prevent cookie theft (so for security purposes).
  37.  
  38.         // Generate new session ids until a unique session id is generated
  39.         while(1){
  40.             $possible_session_id = bin2hex(openssl_random_pseudo_bytes(16));
  41.    
  42.             $query = "SELECT 1 FROM public.user WHERE session_id = :session_id";
  43.             $query_params = array(':session_id' => $possible_session_id);
  44.             try{
  45.                 $stmt = $pdo->prepare($query);
  46.                 $result = $stmt->execute($query_params);
  47.             }catch(PDOException $ex){
  48.                 echo '<span style="color: red">ERROR! Code: 003</span>';
  49.                 exit;
  50.             }
  51.             $row = $stmt->fetch();
  52.    
  53.             // When no row was found with this session id
  54.             if(empty($row)){
  55.                 // The session id is unique and the script must no longer generate new session ids
  56.                 $session_id = $possible_session_id;
  57.                 break;
  58.             }
  59.         }
  60.    
  61.         // Set the session id of the logged in user to the generated session id
  62.         $query = "UPDATE public.user SET session_id = :session_id WHERE user_id = :user_id";
  63.         $query_params = array(':session_id' => $session_id, ':user_id' => $_SESSION['user_id']);
  64.         try{
  65.             $stmt = $pdo->prepare($query);
  66.             $result = $stmt->execute($query_params);
  67.         }catch(PDOException $ex){
  68.             echo '<span style="color: red">ERROR! Code: 004</span>';
  69.             exit;
  70.         }
  71.    
  72.         // Set the new session id as session variable and as value of the session cookie
  73.         setcookie("session_id", $session_id, time()+259200, "/chat/");
  74.     }
  75. }
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement