Clearing Cookies without knowing the names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yesmaster
Forum Newbie
Posts: 2
Joined: Wed Feb 21, 2007 3:29 pm

Clearing Cookies without knowing the names

Post by yesmaster »

Hey guys,
First time posting. Anyone know a way to clear out all the cookies under / without knowing the exact names? The only way I found to clear a cookie is by using setcookie(), but you have to konw the exact name.

I tried clearing the cookies by clearing the array $_COOKIE but that didn't work. Any info appreciated.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You can retrieve the names from $_COOKIE and use them with setcookie().

Code: Select all

foreach($_COOKIE as $name=>$value) {
	setcookie(...
yesmaster
Forum Newbie
Posts: 2
Joined: Wed Feb 21, 2007 3:29 pm

Post by yesmaster »

Thanks. I remember trying this but don't remember having the desired outcome, not sure the details.

What if you don't know the path, in setcookie?
For example

foreach($_COOKIE as $name=>$value) {
setcookie($name, '', time()-42000, '/');
}

So this would clear all the cookie values that were set under path '/'

Would it clear all the cookie values under /aaa/bbb/cc as well?
The manual was not so clear on this.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That wouldn't be all that hard to test... ;)
Post Reply