Jump to content

PHP Destroy cookie?


jamesbrauman

Recommended Posts

I've got something really weird going on. On my computer (localhost, apache web server), setting and my cookies works fine. However, when I put it up on the net, setting cookies works but destroying cookies doesn't.  :o

 

Code for setting cookies:

function user_login($username, $md5password) {
	setcookie("username", $username, strtotime("+90 days"), "/");
	setcookie("md5password", $md5password, strtotime("+90 days"), "/");
}

Code for logging the user out (doesnt work on web server, does on local server):

//Logs a user out.
function user_logout() {
	setcookie("username", "", 1);
	setcookie("md5password", "", 1);
}

 

What is going on?  ???

Link to comment
Share on other sites

Don't worry. The code I have for checking if someone is logged in validates the cookie username and md5 password, so for logging out I just set the cookies as such:

function user_logout() {
	setcookie("username", "", strtotime("+90 days"), "/");
	setcookie("md5password", "", strtotime("+90 days"), "/");
}

 

Thanks.

Link to comment
Share on other sites

It is likely you have a header problem on the page that calls the logout function. Add the following two lines after your <?php tag -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

This code if for logout.php - I didnt add the code you suggested as the code for logout page is only very small.

<?php
include "../construct.php";
user_logout();
header("Location: ../index.php");
exit();
?>

Link to comment
Share on other sites

Still add those lines, and remove the header redirect so that you can see the errors that may be outputted.

 

Ok, added those lines so that logout.php looks like this:

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
include "../construct.php";
user_logout();
//header("Location: ../index.php");
exit();
?>

 

Thought I had fixed the problem before, but it is still there. When I access logout.php on my webserver (which is where user_logout() is not working) no errors are outputted, even with that code.

Link to comment
Share on other sites

Wait you want to logout, so shouldn't it be -90 days and not +90 days. Because to destroy cookies you need to set a date in the past.

 

When deleting a cookie you should assure that the expiration date is in the past.

 

Yes, I was originally trying to delete them like this:

setcookie("username", "", 1, "/");
setcookie("md5password", "", 1, "/");

 

However that wasn't working so thats when I decided to try to set the value to a blank string, but in the future.

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.