Jump to content

PHP won't delete cookies.


sh0wtym3

Recommended Posts

My LOGIN.php has a line that sets the cookies, then redirects the user to MEMBERS.PHP:

setcookie("mysite", $_POST['pass'], time()+3600);

if(isset($_COOKIE['mysite']))
{
header ("Location: members.php");
}

 

And my LOGOUT.PHP page removes the cookies, then redirects the user to LOGIN.PHP:

$past = time() - 3600;
setcookie(mysite, gone, $past);
header("Location: http://mysite.com/login.php");
?>

 

But LOGOUT.PHP won't delete the cookies, and the user keeps getting redirected back to MEMBERS.PHP when they try to log out. Why??

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/143043-php-wont-delete-cookies/
Share on other sites

Try setting the cookie parameters, domain and path.

 

setcookie("mysite", $_POST['pass'], time()+3600, "/", ".yourdomain.com");

And to unset

setcookie("mysite", "", $past, "/", ".yourdomain.com");

 

If you are trying to do this on localhost, let me know and I will post instructions on how to properly set/unset cookies on localhost, as it can be picky. Also it is a good idea to set the time 1 day back due to timezone issues.

 

EDIT:

If using localhost, you would use this to unset the cookies:

To set the cookie:
setcookie("mysite", $_POST['pass'], false, "/", false);

To unset (sort of):
setcookie("mysite", "", false, "/", false);

 

I am not sure why that works with localhost (at least on Apache on Windows). That seems to work for me.

gone needs to be a 0 then the cookie is dead.

 

Makes no difference, cookies are deleted by their time, not their content.

 

Your code:

<?php
$past = time() - 3600;
setcookie(mysite, gone, $past);
header("Location: http://mysite.com/login.php");
?>

 

Should be:

 

<?php
$past = time() - 3600;
setcookie('mysite', gone, $past);
header("Location: http://mysite.com/login.php");
?>

 

Missing quotes.

I've tried every single suggestion in this thread but it still won't work :(

 

The last one I tried was the localhost version you suggested premiso. My code is currently:

 

LOGIN.PHP

<?php
// if login is ok then we start session
setcookie("mysite", $_POST['pass'], false, "/", false);

if(isset($_COOKIE['mysite']))
{
header ("Location: members.php");
}
?>

 

LOGOUT.PHP

<?php
$past = time() - 86400;
//this makes the time in the past to destroy the cookie, set to 1 day before
setcookie("mysite", "", false, "/", false);
header("Location: login.php");
?>

 

I've used this same code on some php pages in my public_html folder and they work fine.

However, this login.php and logout.php are located in a subdirectory within my public_html folder:

 

public_html/members/

 

I don't know if this has anything to do with why it may not be working.

 

try this please.

<?php
setcookie("mysite", "", time() - 3600, "/");
header("Location: http://mysite.com/login.php");
?>

 

or this

<?php
if(isset($_COOKIE['mysite'])){
setcookie("mysite", "", time() - 3600, "/");
header("Location: http://mysite.com/login.php");
exit;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.