hello i`m running crazy through a problem that i tried to fetch.
i can not delete a cookie that will be set on index.php like that:
$a = 'text';
setcookie('name_of_cookie', $a, time()+60*60*24*365);
on page-php i can call that cookie and the content is like $a. i tried to delete that different ways on page.php like that:
setcookie('name_of_cookie', FALSE, time()-3600);
i also found a version where i only set it to:
setcookie('name_of_cookie');
what just will set a another cookie with the same name like i saw in the settings of the browser. that new cookie has no content - sure, there is no content declared.
i also tried to set a higher time in the past because of the server location but this doesnt work as well. even trying to set the time to just 1
does not work.
i checked the spelling of the name sveral times, there is no failure that comes from different names.
i would be glad if someone could tell what am i doing wrong?
thanks.
When you set cookies be very specific how do you set them. Always set domain name (which can be taken from $_SERVER['SERVER_NAME']) and path (which I usually set to "/"). If you are not consistent something can take "clever" initiative and break your logic. It happens often when you set cookies in JS and try to remove them from PHP (or the other way around). You might have multiple cookies with the same name but different subdomain or path.
For example you set cookies like this:
setcookie( 'Foo', 'Bar', time() + 3600, '/', $_SERVER['SERVER_NAME'] );
And you unset them like this:
setcookie( 'Foo', '', time() - 999999, '/', $_SERVER['SERVER_NAME'] );
Missing 2 last arguments will put you in a situation you are at the moment.
Delete every cookies as below.
if (isset($_SERVER['HTTP_COOKIE']))
{
$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
foreach($cookies as $cookie) {
$parts = explode('=', $cookie);
$name = trim($parts[0]);
setcookie($name, '', time()-1000);
setcookie($name, '', time()-1000, '/');
}
}