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);
Did you check if your script already send its HTTP headers?
if (headers_sent()) {
trigger_error("Cant change cookies", E_USER_NOTICE);
}
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.
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).
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);
}
The manual states:
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.
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