View Full Version : Unsetting Sessions/Cookies

12-10-2007, 12:35 PM
Hi all,

I currently have a login area with the remember me features, this works fine but im having trouble getting the logout feature to work. I have set 2 cookies 'user_name' & 'password' and one session called 'loggedin'. I have tried the following code on a page called logout.php


setcookie('user_name', '', time()-60);
setcookie('password', '', time()-60);
unset ($_SESSION['loggedin']);
header("Location: http://www.mysite.com");

But although the redirect works the user isnt logged out, which would suggest that the session and/or cookies are not being reset. Can anyone see why this may be the case?

Thanks in advance

12-10-2007, 12:51 PM
Dude... is that an html question? I think not. Try to look which forum this would best be posted in before you post it! I think this needs to be placed in the php forum. However, sdon't multipost... a nice moderator will move it for you.

In answer to the question, to unset cookies you have to pasre them the exact same parameters that you set them with. To 'destroy' a session use:


This is what I use:

setcookie('Yourcookie', 'yours', time()-300, '', '', 0);
$_SESSION = array();

12-10-2007, 03:08 PM
Appologies for posting in the wrong place:

This is how i set the cookies:

setcookie("clubviewnetwork[user_name]", $user_name, $time + 3600);
setcookie("clubviewnetwork[password]", $password, $time + 3600);

This is how im trying to delete them:

setcookie("clubviewnetwork[user_name]", "", time()-3600);
setcookie("clubviewnetwork[password]", "", time()-3600);

As far as i can see they are parsed exactly the same way, but it still doesnt seem to want to remove them. I'm new to PHP so maybe im just missing something very obvious???

The session was set like this:

$_SESSION['loggedin'] = 1;

I'm trying to remove the session with the following code:


Could this be the reason the user remains logged in even after pressing the logout button?

Thanks for the help