Jump to content

Expire ALL cookies from domain / path?


Akkari

Recommended Posts

Hey there everyone.

 

I'm getting at a point in my application where I need to clear all cookies set by my domain / path before proceeding. There are a bunch of different cookies on my domain which are set for a hell lot of different things, so I did not find it logic to use something like:

 

if(isset($_COOKIE['certain_cookie'])) setcookie("certain_cookie","value", time());

 

For each and every cookie that could be set to make it expire.

 

So what do you suggest?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/203743-expire-all-cookies-from-domain-path/
Share on other sites

Clue was in your description:

 

For each and every cookie that could be set to make it expire.

 

Assuming you want to remove *every* cookie, you could try something like:

 

if (!empty($_COOKIE))
{
    foreach ($_COOKIE as $name => $value)
    {
        setcookie($name, $value, time() -1);
    }
}

 

Edit:

 

You could also introduce an array_key_exists check using an array of cookies to remove if you only want to target certain cookies, or alternatively omit some from the removal.

MrAdam,

 

Thanks a bunch. However, I'm pretty poor at for each loops anyway so could you help me better understand the code? Specifically this part:

 

foreach ($_COOKIE as $name => $value)

 

Where did the $name and $value variables come from? And why do we compare them by the "=>"?

 

Yes, I would like to omit one from the removal  :P I just didn't want to make things more complicated. I have had a quick read in the php.net manual. I assume I'd use it like this?

 

array_key_exists('my_cookie_name, $_COOKIE)

 

But how would I incorporate it into the foreach loop so that it does not get deleted?

 

Thanks and sorry for the stupid questions  :-[

 

@ FD_F

 

Thanks for taking the time to post, however, perhaps it was because of my kinda crappy description, you could not understand what I actually meant.

 

Thanks again everyone. :)

 

 

 

foreach ($_COOKIE as $name => $value)

 

All that does is assign the current element's key ($name) and value ($value) in the array to those variables, then advances the array pointer for the next iteration.

 

"=>" is not a comparison operator, but an assignment operator. You can read more on those in the manual.

 

It's pretty simple to incorporate the key check. Just to check for a single key:

 

if (!empty($_COOKIE))
{
    foreach ($_COOKIE as $name => $value)
    {
        if ($name == 'special_key')
        {
            continue;
        }

        setcookie($name, $value, time() -1);
    }
}

 

Personally though I'd go with this second option (in future it'd be easier to expand):

 

$special_keys = array('special_key1', 'special_key2');

if (!empty($_COOKIE))
{
    foreach ($_COOKIE as $name => $value)
    {
        if (in_array($name, $special_keys)
        {
            continue;
        }

        setcookie($name, $value, time() -1);
    }
}

 

Sorry, in_array would be used in this example, not array_key_exists().

Archived

This topic is now archived and is closed to further replies.

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