public function get_cookie($cookie_name) { $cookie_plaintext = $_COOKIE[$cookie_name]; // Username will be in $array[0], hashed password in $array[1] // MAC in $array[2] and cookie expiration in $array[3] $array = array(); $array = explode('|', $cookie_plaintext); // The reason we have "::" is because normally you cannot have // multiple constructors, but we can use the factory pattern to do it // Note: When using the factory pattern, you don't use the "new" keyword! $cookie = Cookie::retrieve($array[0], $array[1], $array[2], $array[3]); return $cookie; }
public static function getLoggedIn() { $c = Cookie::retrieve('cyberfish_login'); if (!$c) { return false; } list($login, $loginExpireAt, $hash) = explode('|', $c->getParam('id')); if (time() > $loginExpireAt) { return false; } $uid = self::findByLogin($login); if (!$uid) { return false; } $user = new User($uid); $key = hash_hmac('md5', $user->loginName() . '|' . $loginExpireAt, $user->salt()); if ($hash != hash_hmac('md5', $user->loginName() . '|' . $loginExpireAt, $key)) { return false; } self::updateLastLoginTime($uid); return $user; }