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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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;
}
?>

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.