當用戶登錄時,這裏是我的用戶功能:在PHP中設置的Cookie;但沒有被發現?
public function login($user) {
global $database;
if ($user) {
$_SESSION['user_id'] = $user->id;
$this->user_id = $_SESSION['user_id'];
$_SESSION['username'] = $user->username;
$this->username = $_SESSION['username'];
setcookie('user_id', $this->user_id, time() + (60 * 60 * 24 * 14));
setcookie('username', $this->username, time() + (60 * 60 * 24 * 14));
$this->logged_in = true;
}
}
當我看Cookiees在Chrome中,我找到兩個與此餅乾:
1 USER_ID,1用戶名。
但是,當瀏覽器被關閉,我試圖回來,它不會檢測cookiee:這裏的過程是:
class Session {
// Most of the class has been edited out; the code above is also a method in this clas. Removed so it's not duplicated.
private $logged_in = false;
public $user_id; // yes I realize this is insecure
public $username; // yes I realize this is insecure
function __construct() {
session_start();
$this->check_login();
}
public function is_logged_in() {
return $this->logged_in;
}
private function check_login() {
if (isset($_COOKIE['user_id']) && (isset($_COOKIE['username']))) {
$_SESSION['user_id']= $_COOKIE['user_id'];
$_SESSION['username'] = $_COOKIE['username'];
} else { // When I test, below shows up showing it doesn't think Cookie is set.
echo "Cookie not set in check_login().<br />";
}
if (isset($_SESSION['user_id'])) {
$this->user_id = $_SESSION['user_id'];
$this->username = $_SESSION['username'];
$this->logged_in = true;
} else {
unset($this->user_id);
$this->logged_in = false;
}
}
$session = new Session();
}
'print_r($ _ COOKIE);'在設置cookie之後,在下一次加載時打印什麼?2)在關閉並重新打開瀏覽器之後? – Czechnology
我登錄後會顯示正確的cookie名稱和值。這:[user_id] => 1 [用戶名] => Joe用戶。它還顯示PHPSESSID和一些Google分析餅乾。當我重新打開瀏覽器。它只顯示Google Cookie。 – KickingLettuce