Jesse
Jesse

Reputation: 231

Can't remove cookies, which are generated by PHP

Here is how I create cookie in PHP and Javascript

PHP, before loading a page, I first create cookie via PHP .

setcookie('my_key', $value, 0,ADMIN_COOKIE_PATH);

Javascript. I'm actually using jquery with this cookie plugin.

When dropdown changed, I change the cookie value.

jQuery.cookie("my_key", selected);

In Google Chrome, everything works as what I expected.

The value of my_key cookie will be altered when dropdown changed.

In firefox, it generates another identical cookie.

yes, it is identical to the my_key cookie generated by PHP:

same name, same expire(browser session), same domain, same path, same httponly(blank), same security(blank)

The only difference is the new cookie is set to new value.

I try jQuery.removeCookie('my_key') but it can only delete the new cookie.

I'm not sure if it is a bug of firefox, or I did something wrong. But, truly, it is an annoying issue.


Update 1: it's true that I didn't specific path in javascript and right now I can change the cookie which is generated by PHP when I set the path. But still not able to delete the cookie via JS.

FYI, I'm able to delete cookies in PHP but just can't delete them in JS.

Upvotes: 2

Views: 3668

Answers (1)

Jesse
Jesse

Reputation: 231

Answer inspired by Spokey and MightyPork:

  • 1, set path to jQuery.cookie

  • 2, use jQuery.cookie("my_key", '',{expires: -1,path:'/admin'}); instead of jQuery.removeCookie

Update:

I did use jQuery.removeCookie('my_key',{path:'/admin'}) last night but not working. And I just now try again this morning, it's working. Maybe I was too tired last night.

Conclusion

The whole thing is about Path. I think, from my tests, perhaps, firefox has more strict standards than google chrome about cookie manipulation.

Upvotes: 4

Related Questions