我使用PHP Session
變量有一個用戶登錄並編輯他們的網站。當他們註銷時,我呼叫session_destroy();
函數來終止會話。會話和cookie,如果我銷燬會話,cookie會消失嗎? (PHP)
在登錄屏幕,我一定要「記住此計算機」和而不是使用$_SESSION[]
變量我設置這樣一個cookie的選項:
setcookie("admin", true, $expire);
的Cookie設置,但是當我註銷後,Cookie將取消設置。任何方式來防止這種情況?我希望會議結束,但我希望網站也記住計算機。
謝謝你的時間。
編輯:這裏有兩種方法我用它來啓動和銷燬會話(會話方法我稱之爲是另一個類)
public function loginCheck() {
//check if the remember me is set
if (isset($_POST['remember'])) {
$expire = time() + 60 * 60 * 24 * 30;
setcookie("admin", true, $expire);
}
else{
setcookie("admin", "", time()-3600);
}
$sth = $this->db->prepare("SELECT uid FROM philllwareusers WHERE
usr = :login AND pasw = :password");
$sth->execute(array(
':login' => $_POST['user'],
':password' => $_POST['pswrd']
));
$data = $sth->fetch();
$count = $sth->rowCount();
if ($count > 0) {
//check if user has permision to edit this website
$sth = $this->db->prepare("SELECT web_id FROM website_spine WHERE
admin_id = :uid ");
$sth->execute(array(
':uid' => $data['uid']
));
$datas = $sth->fetch();
$counts = $sth->rowCount();
if ($counts > 0) {
if ($datas['web_id'] == WEB_ID) {
Session::init();
Session::set('uid', $data['uid']);
Session::set('loggedIn', true);
header('location: ../index');
} else {
header('location: ../Adminlogisn');
}
}
// login
} else {
header('location: ../Adminlogin');
}
}
function logout() {
Session::init();
Session::destroy();
header('location: '.URL.'/Adminlogin');
}
這是管理員登錄的樣子(的一部分會檢查如果cookie應設置,並應留一套或摧毀它。),
<?php
if(!isset($_COOKIE['admin']) || $_COOKIE['admin'] == ''){?>
Remember this computer as an admin computer <div style="display: inline;cursor: pointer;" onclick="alert('By selecting this option, your website will remember this computer and present the login option on the menu bar when you are not logged in.')">[?]</div>
<input type="checkbox" name="remember" style="width: 20px;"/>
<?php
}
else{
?>
Un-check to forget this computer as an admin computer. <div style="display: inline;cursor: pointer;" onclick="alert('This computer is currently rememberd as and admin computer, making the login link on the menu bar visible for easy access. Uncheck this box to forget this computer as admin computer.')">[?]</div>
<input type="checkbox" name="remember" checked="checked" style="width: 20px;"/>
<?php
}
?>
如果單獨設置cookie,它不會消失。看起來你在會話中檢查了你的cookie的價值已經被破壞了。 – Leri 2012-08-04 16:37:41
如果在用戶訪問該頁面時未提交「記住」,則取消設置Cookie。也許這會導致cookies丟失。 – Leri 2012-08-04 16:41:18
如果cookie已經存在,則記錄會自動設置,用戶可以通過取消選中來刪除cookie,因此它會丟失頁面。對於之前沒有提及的問題,我非常抱歉,請查看我修改後的問題。 – 2012-08-04 16:45:07