Hi everyone,

Got a pesky little issue here with cookies. I have searched numerous forums and numerous posts of how to delete a cookie. So, I am much aware of this doing the trick:

setcookie('cookie_name');

Basically where not assigning any value to a cookie will delete it. (This is just to save some time :P)

Anyways, I wish this was the case, but it's not working. I got it to work on a different portion of my application. However I had to come at it a different angle.

Basically, the way I wanted it to be was a user clicks the "logout" button and the cookies would be deleted and after being deleted, the user would be re-directed to the login page. But, instead I had to make a "logout.php" where the user is directed to this page, the delete cookie works fine, THEN gets directed to the login page.

I would use this technique for my current task in my application, but this would greatly complicate things.

Thanks in advance for any help.

Recommended Answers

All 9 Replies

setcookie('cookie_name', time() - 42000);

actualy its

setcookie('cookie_name', null, time() - 3600);

actualy its

setcookie('cookie_name', null, time() - 3600);

*sigh*


See, I know this line of code should be working but it's just not, this is so frustrating. Does this line of code only work in IE or something?

Do you have a re-direct on the logout.php script? If you do comment this out and run the code to see if the cookies are being deleted at all, its possible depending on how your script works that it is re-creating the cookies when the user is re-directed.

The code is not browser defendant since PHP is a server side language not client side.

Personally I use the following code to delete cookies, but it does exactly the same as the previous examples:

$past = time() - 3600;
	setcookie('cookie_name', null, $past);

try installing Firebug in Firefox and watch the console to see if the cookie is deleted or not
and after that is it recreated or not

Useful Plugins :
- Firebug
- Firecookie
- Web Developer

I'd like to thank those who took the time to share their knowledge/input. Oddly enough it just decided to work, I might have done something I'm not sure about that fixed it, but all is good now (yay for programming lol).

Also, I love Firebug iPHP, so no worries there ;)

Hello,

I realise this has now been resolved.. but is it possible that the problem was something to do with the path of your cookies?

For example: Your login page was creating a cookie and directing the user to the members page within a subdirectory, then the logout page was trying to delete a cookie which existed in the subdirectory instead of the root where it was created?

Pixel =)

use setcookie ($name, null, time()-3600, '/');

to ensure the cookie is wiped entirely ('/' refers to the path of the site)...
this should cleanup the cookie in the right way.

worked for me :)

-GJ

commented: Brother you saved my precious time. And its 2022. still helpful code. +0

@barkgj

Genious!!!!! It worked for me. The key was using '/' as the last parameter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.