Jump to content

Cookie won't die...


benphp

Recommended Posts

I set a cookie on page1.php:

 

setcookie("wusername",$_REQUEST['wusername']);

 

I have another page logout.php for logging out that kills the cookie and sends me to the login page.

 

if (isset($_REQUEST['btnLogOut'])) {
setcookie ("wusername", "", time() - 3600);
header("Location: login.php");
}

 

However, when I go to the login page, login.php, I have this:

 

if (isset($_COOKIE['wusername'])) {
$wusername = "Logged in as: " . $_COOKIE['wusername'];
}

 

Which does indeed retain my cookie and prints it. Why is my cookie not dying?

Link to comment
Share on other sites

Cookies are destroyed by your browser, all setcookie does is sent a request to the browser to set the cookie - be it in the past or future. The browser then removes any expired cookies.

 

Have you confirmed the setcookie(foo, bar, time-3600) is definitely being executed? I find it hard to believe your browser isn't functioning properly unless you've developed your own browser.

Link to comment
Share on other sites

What do you mean wont die? A couple of months ago I was doing some stuff with a cookie that would cease when the browser was closed and look and it was still present in name after I closed and reopened the browser however the cookie would not work on the site, it had expired. I can't remember which browser using.

 

PS. open your browser and click the cookie name, it will tell you when it expires.  If it is a past date or time it is no good, it's just listed in the browser waiting to be deleted.

Link to comment
Share on other sites

Actually, come to think of it... cookies are set via HTTP header... if you're redirecting in the same header, that might cause problems / confuse the browser.

 

You may need to use a meta refresh instead to eliminate the race condition.

 

Better idea would be to use your session to store login data instead of a cookie.  It's a more secure solution anyway, as the value is stored on the server, and can't be hacked as easily.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.