Beispiel #1
0
 /**
  * Write remember-me token into database and into cookie
  * Maybe splitting this into database and cookie part ?
  *
  * @param $user_id
  */
 public static function setRememberMeInDatabaseAndCookie($user_id)
 {
     if (self::$setRememberMeTokenQuery === null) {
         self::$setRememberMeTokenQuery = DatabaseFactory::getFactory()->getConnection()->prepare("UPDATE users SET user_remember_me_token = :user_remember_me_token WHERE user_id = :user_id LIMIT 1");
     }
     // generate 64 char random string
     $random_token_string = hash('sha256', mt_rand());
     // write that token into database
     self::$setRememberMeTokenQuery->execute(array(':user_remember_me_token' => $random_token_string, ':user_id' => $user_id));
     // generate cookie string that consists of user id, random string and combined hash of both
     $cookie_string_first_part = $user_id . ':' . $random_token_string;
     $cookie_string_hash = hash('sha256', $cookie_string_first_part);
     $cookie_string = $cookie_string_first_part . ':' . $cookie_string_hash;
     // set cookie
     setcookie('remember_me', $cookie_string, time() + Config::get('COOKIE_RUNTIME'), Config::get('COOKIE_PATH'));
 }