I am currently developing a site, and am having some problems with logging my users out. Now when I developed this at home it worked fine, but once I uploaded it it doesn't seem to work. Now, I haven't had much success at getting my choice of forum to post in correctly recently but I am pretty sure this is PHP!..
My code looks like so;
if(isset($_POST['btnLogout']))
{
//Remove the cookie
$pageUserObject->ClearCookie();
unset($_SESSION['thisUserSession']);
header ('Location: <!-- m --><a class="postlink" href="http://'">http://'</a><!-- m --> . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php');
exit();
The clearcookie function looks like so;
function ClearCookie()
{
if(isset($_COOKIE["userData"]))
{
setcookie("userData", $usrId, time() - 3600);
setcookie("userLastVisit", $this->mv_lastForumVisited, time() - 3600);
}
}
But this isn't working now, it redirects but the user is still logged in suggesting it isn't getting rid of the cookies. Any ideas?
Let me know if this is insufficient information;you clear it like you set it, but the time is just the opposite.
setcookie("userData", $usrId, time() - 3600);
if you set it with time() + 3600 than it should work. also you should be setting the value to null or ""
setcookie("userData", "", time() - 3600);
but then again, it maybe that your path to the cookie is wrong, try this
setcookie("userData", "", time() - 3600,"/");Cookies expire according to the local (client-side) time right? Because you are taking the server-time, and subtracting 1 hr (3600secs = 60secs*60mins = 1hr). If the clients time is off from the server time by more than an hr, that would not unset it right away.
Also, scoutt is right...everything has to be the same as when you set the cookie (except the time, and the data). so if you set it like this:setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1); then you have to unset it like this:setcookie("TestCookie", '', time()-3600, "/~rasmus/", ".example.com", 1);
And the time doesn't really have to JUST change from adding to subtracting...you just have to set it long enough ago that the computer it is on knows that it is expired, and treats it as such. IF the server time and client time were the exact same, time()-1 would be sufficient.
Read Example 1 and Example 2 here (<!-- m --><a class="postlink" href="http://www.php.net/manual/en/function.setcookie.php">http://www.php.net/manual/en/function.setcookie.php</a><!-- m -->)Well sort of.... they are actually set to GMT time. which your system is responsible for knowing. EST = -5 hr. They are set to GMT on that date, if your 5 hours behind the that then your going to expire in when you system says it that time in GMT.Thanks guys for your really helpfuly replies, my problem was simply I wasn't putting the "/" at the end of the call.. why can't it just error or something.
Thanks to you guys though, I have learnt a lot through this!
My code looks like so;
if(isset($_POST['btnLogout']))
{
//Remove the cookie
$pageUserObject->ClearCookie();
unset($_SESSION['thisUserSession']);
header ('Location: <!-- m --><a class="postlink" href="http://'">http://'</a><!-- m --> . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php');
exit();
The clearcookie function looks like so;
function ClearCookie()
{
if(isset($_COOKIE["userData"]))
{
setcookie("userData", $usrId, time() - 3600);
setcookie("userLastVisit", $this->mv_lastForumVisited, time() - 3600);
}
}
But this isn't working now, it redirects but the user is still logged in suggesting it isn't getting rid of the cookies. Any ideas?
Let me know if this is insufficient information;you clear it like you set it, but the time is just the opposite.
setcookie("userData", $usrId, time() - 3600);
if you set it with time() + 3600 than it should work. also you should be setting the value to null or ""
setcookie("userData", "", time() - 3600);
but then again, it maybe that your path to the cookie is wrong, try this
setcookie("userData", "", time() - 3600,"/");Cookies expire according to the local (client-side) time right? Because you are taking the server-time, and subtracting 1 hr (3600secs = 60secs*60mins = 1hr). If the clients time is off from the server time by more than an hr, that would not unset it right away.
Also, scoutt is right...everything has to be the same as when you set the cookie (except the time, and the data). so if you set it like this:setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1); then you have to unset it like this:setcookie("TestCookie", '', time()-3600, "/~rasmus/", ".example.com", 1);
And the time doesn't really have to JUST change from adding to subtracting...you just have to set it long enough ago that the computer it is on knows that it is expired, and treats it as such. IF the server time and client time were the exact same, time()-1 would be sufficient.
Read Example 1 and Example 2 here (<!-- m --><a class="postlink" href="http://www.php.net/manual/en/function.setcookie.php">http://www.php.net/manual/en/function.setcookie.php</a><!-- m -->)Well sort of.... they are actually set to GMT time. which your system is responsible for knowing. EST = -5 hr. They are set to GMT on that date, if your 5 hours behind the that then your going to expire in when you system says it that time in GMT.Thanks guys for your really helpfuly replies, my problem was simply I wasn't putting the "/" at the end of the call.. why can't it just error or something.
Thanks to you guys though, I have learnt a lot through this!