Reputation: 3822
I've tried searching the php manual and internet on how to delete cookies and I've tried it the exact same way they all say:
setcookie("name", '', 1);
or
setcookie("name", '', time()-3600);
But when I check the cookies in the cookies dialog in Firefox, it's still there with the same value. I set this cookie using the following line:
setcookie("name", $value, time() + 259200, $path);
I found this question on stackoverflow: , but none of the answers solved the problem. I also tried putting all paramaters in, like the author said, but it had no effect.
Does anyone see the problem?
Upvotes: 37
Views: 53606
Reputation: 10302
this is my experience with cookie that, the cookie may not be deleted from the client machine until the browser window (that we use to see existing cookie) is closed. So close that window and the try your code.
Upvotes: 0
Reputation: 316
Just like is said in the correct answer (I want it to send an updated one), to unset, every parameter used to set the cookie is necessary, even secure and httponly
Set
setcookie("name_cookie", $name_value, 0, '/', $domain, false, true);
Unset
setcookie("name_cookie", '', time()-1000, '/', $domain, false, true);
Upvotes: 1
Reputation: 1
var remember = $.cookie('auto_login');
if (remember == 'true') {
var username = $.cookie('username');
var password = $.cookie('password');
$('#username').val(username);
$('#password').val(password);
}
$('#logsub').click(function (event) {
if ($('#auto_login').is(':checked')) {
var username = $('#username').val();
var password = $('#password').val();
// set cookies to expire in 14 days
$.cookie('username', username, {expires: 14});
$.cookie('password', password, {expires: 14});
$.cookie('auto_login', true, {expires: 14});
} else {
// reset cookies
$.cookie('username', null);
$.cookie('password', null);
$.cookie('auto_login', null);
}
});
Upvotes: -1
Reputation: 500
I'm surprised no one has mentioned it (or maybe I missed it), but domain is important too! If you are on sub-domain.example.com, and the cookie is from .example.com, then you need to explicitly set the domain parameter, otherwise it will assume the current domain and it won't work.
setcookie('cookiename', FALSE, -1, '/', '.example.com');
Sub-domains value will not clear cookies from parent domain.
Upvotes: 16
Reputation: 78
This did the trick for me:
setcookie("brownie","",1,'/');
unset($_COOKIE["brownie"]);
Upvotes: 2
Reputation: 326
set a cookie
setcookie('cookiename', $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
unset cookie
setcookie('cookiename', '', time() - 3600, "/");
No need to panic. Just copy function you use to set cookie and now minus the time. Do not get confuse, make it easy and clear.
Upvotes: 0
Reputation: 309
If you delete cookie for the specific path and your path parameter ends with the trailing slash '/' then it will work in Firefox and IE, but won't work in Chrome and Opera. If there is no trailing slash then it will only work in Chrome and Opera.
So you should use both:
setcookie('cookiename', '', time() - 60*60*24, $chatPath); // WebKit
setcookie('cookiename', '', time() - 60*60*24, $chatPath . '/'); // Gecko, IE
Upvotes: 7
Reputation: 2035
Just define a custom function in global core functions file like global.php
function delete_cookie()
{
unset($_COOKIE['cookiename']);
setcookie('cookiename',NULL,time()-3600, '/');
return true;
}
and use this function at the top of the html code like
include('global.php')
if(isset($_GET['delete_cookie']))
{
delete_cookie(); //if you want to pass the parameters into the function also possible like delete_cookie(param1);
}
Upvotes: 0
Reputation: 356
Sometimes you saved the cookie in a different path than you're trying to delete/uset it in.
Go into eg. Chrome cookie settings and check the cookie path, then add the path to the setcookie command, and delete it like this:
setcookie( "my_cookie_name","",1,'/mypath');
Trying to delete or unset a cookie that is saved in the wrong path will not work and can be very frustrating.
Upvotes: 0
Reputation: 1703
I'm surprised no one has posted this yet, but this works perfectly for me:
To CREATE or CHANGE cookie by name:
$_COOKIE['myCookieName'] = 'I can be changed to whatever u want';
To DELETE a cookie by name:
unset($_COOKIE['myCookieName']);
Upvotes: -1
Reputation: 3038
I had a similar issue.
I found that, for whatever reason, echoing something out of logout.php made it actually delete the cookie:
echo '{}';
setcookie('username', '', time()-3600, '/');
Upvotes: 0
Reputation: 3822
Ok, I really don't understand, but it works now. The magic code is:
setcookie("name", '', 1, $path);
Haven't I already tried that??! Whatever, it works now. Thanks for your help, people!
Upvotes: 15
Reputation: 38252
Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string, or
FALSE
, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client. This is internally achieved by setting value to 'deleted' and expiration time to one year in past.
So also make sure that $path
is specified correctly -- also when deleting it. For instance, if the cookie was specified in a subdirectory, you may not be able to delete it from either the parent or children directories (or both).
I'm not entirely sure how the permissions work, but you might want to use the Web Developer Toolbar to view what the path is of the cookie you are attempting to delete.
Upvotes: 73
Reputation: 10880
Happens to me as well one in ten times though. I guess its a problem with the way we code.
This is my code
setcookie("token", "", time() - 36000, "/");
Upvotes: 0
Reputation: 29897
Did you check if your script already send its HTTP headers?
if (headers_sent()) {
trigger_error("Cant change cookies", E_USER_NOTICE);
}
Upvotes: 3
Reputation: 3566
I tried using
setcookie("name", "", -1);
and on my server with Apache/PHP5 it cleared the cookie (at least a var_dump($_COOKIE) showed an empty array).
Upvotes: 2
Reputation: 766
Have you tried setting the time to a small value and using a value for cookie?
setcookie("name", 'n', 1);
Upvotes: 0