Akkari Posted June 3, 2010 Share Posted June 3, 2010 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 More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 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. Link to comment https://forums.phpfreaks.com/topic/203743-expire-all-cookies-from-domain-path/#findComment-1067119 Share on other sites More sharing options...
FD_F Posted June 3, 2010 Share Posted June 3, 2010 you can try work with .htaccess http://www.internetblog.org.uk/post/910/how-to-set-cookie-expiration-in-apache Link to comment https://forums.phpfreaks.com/topic/203743-expire-all-cookies-from-domain-path/#findComment-1067120 Share on other sites More sharing options...
Akkari Posted June 3, 2010 Author Share Posted June 3, 2010 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 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. Link to comment https://forums.phpfreaks.com/topic/203743-expire-all-cookies-from-domain-path/#findComment-1067125 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 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(). Link to comment https://forums.phpfreaks.com/topic/203743-expire-all-cookies-from-domain-path/#findComment-1067129 Share on other sites More sharing options...
Akkari Posted June 3, 2010 Author Share Posted June 3, 2010 Wow, thank you so much Link to comment https://forums.phpfreaks.com/topic/203743-expire-all-cookies-from-domain-path/#findComment-1067130 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.