Reputation: 4437
I use the PHP Session
variable to have a user login and edit their website. When they logout I call the session_destroy();
function to terminate the session.
At the login screen, I have an option to 'remember this computer' and instead of using a $_SESSION[]
variable I set a cookie like this:
setcookie("admin", true, $expire);
The cookie is set, but when I logout, the cookie unsets. Any way to prevent this? I want the session to end, but I would like the website to remember the computer as well.
Thank you for your time.
Edit: Here are the two methods I use to initiate and destroy the sessions (the session methods I call are on another class)
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');
}
this is what the admin login looks like (the part that will check if the cookie should be set, and should stay set or destroy it.),
<?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
}
?>
Upvotes: 0
Views: 1808
Reputation: 24
here is the link in which you can find the answer of your question:
http://www.codeflask.com/2012/08/why-session-destroy-when-remove-my.html
And the actual answer (cited from the link) is:
Session is destroyed automatically when cookies removed the reason is whenever the session is created it also stores its id in a cookie so that's why session destroyed automatically.
Upvotes: -1
Reputation: 3034
Without checking your code.
You will have to check if the user has a cookie (not a session!). If he goes into the website with a cookie but without a session you log him in (and set the session) in the background.
The cookie shouldn't unset if you have a appropriate expire time. The session cookie however, will disappear after the browser is closed.
Upvotes: 0