0
votes

My problem cookie is not unset() properly.

Here is my code

$expire=time()+(60*60*24*7);
setcookie('ppt-superadmin',$user['login_id'],$expire,"/",$baseurl)

here $baseurl="http://localhost/demo/

This is working fine. And here is my logout.php code

session_start();
$expire=time()-(60*60*24*7);
unset($_SESSION['ppt-superadmin-login']);
setcookie('ppt_superadmin',"",$expire,"/",$baseurl);
unset($_COOKIE['ppt-superadmin']);
session_destroy();

I have tried both unset and setcookie() to a past time. BUt not working. I have echo its value, its showing value set at the time of login. What is the problem here??

1
Do you have display_errors turned on? Always when developing code error_reporting(E_ALL); ini_set('display_errors', 1); -- header errors are extremely common with cookie problems. Also, where did you check the unset cookie's value? Was it in the same script? You will not see it unset until a subsequent HTTP request. Best to inspect the cookie with your browser's developer tools rather than look at its value in PHP. - Michael Berkowski
Ah, $baseurl should be a domain localhost, not a URL http://localhost/... - Michael Berkowski
To unset a cookie, you just set the cookie again with an expiry timestamp in the past, like time()-1 But why use a cookie, if you have a session? - Elias Van Ootegem
Used cookie for remember me function - Girish Sasidharan
@MichaelBerkowski Yes its turned on. Not showing any error. - Girish Sasidharan

1 Answers

1
votes

I guess you have a a typo,

setcookie('ppt_superadmin',"",$expire,"/",$baseurl);
              ^

Make sure it's underscore or dash because at time of setting you have name with dash while at time of un-setting it, you used underscore.