How do you update a cookie in PHP?
Asked Answered
T

5

41

If I call setcookie() two times with the same cookie name, I get two cookies created.

How do you update an existing cookie?

Transcontinental answered 26/6, 2011 at 23:57 Comment(3)
How did you confirm that you get two cookies? – Harvestman
I right click -> edit site preferences -> cookies (Opera), and there I see lots of cookies with the same name – Transcontinental
This account ("Cookie") was legit created just to post 2 questions about cookies πŸ˜‚ – Cantrell
A
25

You can't update a cookie per se, you can however overwrite it. Otherwise, this is what you are looking for: http://php.net/manual/en/function.setcookie.php

It works. Be sure to read "Common Pitfalls" from that page.

You can use the super global $_COOKIE['cookie_name'] as well to read cookies.

Accept answered 27/6, 2011 at 0:4 Comment(8)
but it doesn't work :(( set_cookie('fuuuuu', rand(0, 3434543), $exp_date); , after refresh I get another cookie with the same name, different value :| – Transcontinental
Are they from the same domain? www or no-www counts. – Accept
hmm I didn't set the domain argument.. But the cookies are set from the pages of one domain – Transcontinental
You don't need to specifically set the domain if they are from the same domain. – Accept
It's worth always setting the path param though, that might be the issue here – Annice
ok it works if I add the path argument (I added /), I dont understand what's that for?? – Transcontinental
The path param restricts the path of the url with which the cookie can be used. If you set it to something like '/forum', then the cookie is only available on pages that are at or above the '/forum' directory. If you do '/' then it's the root path, meaning all pages in all directories have access to it. – Subplot
we cannot write by $_COOKIE['cookie_name'] to modify cookies, edit your answer – Analogy
C
53

You can update a cookie value using setcookie() function, but you should add '/' in the 4th argument which is the 'path' argument, to prevent creating another cookie with the same name.

i.e. setcookie('cookie_name', 'cookie_value', time()+3600, '/');

A suggested expiration time for the 3rd argument:

  • $exp_time = time()+3600; /* expire in 1 hour */
  • $exp_time = time()+86400; /* expire in 1 day */
Conscious answered 24/6, 2013 at 14:43 Comment(3)
From the PHP docs: If set to '/', the cookie will be available within the entire domain. The default value is the current directory that the cookie is being set in. – Campos
Why is this not the voted answer? – Margetmargette
Setting the domain (5th parameter) to match the existing cookie matters as well. – Zoography
A
25

You can't update a cookie per se, you can however overwrite it. Otherwise, this is what you are looking for: http://php.net/manual/en/function.setcookie.php

It works. Be sure to read "Common Pitfalls" from that page.

You can use the super global $_COOKIE['cookie_name'] as well to read cookies.

Accept answered 27/6, 2011 at 0:4 Comment(8)
but it doesn't work :(( set_cookie('fuuuuu', rand(0, 3434543), $exp_date); , after refresh I get another cookie with the same name, different value :| – Transcontinental
Are they from the same domain? www or no-www counts. – Accept
hmm I didn't set the domain argument.. But the cookies are set from the pages of one domain – Transcontinental
You don't need to specifically set the domain if they are from the same domain. – Accept
It's worth always setting the path param though, that might be the issue here – Annice
ok it works if I add the path argument (I added /), I dont understand what's that for?? – Transcontinental
The path param restricts the path of the url with which the cookie can be used. If you set it to something like '/forum', then the cookie is only available on pages that are at or above the '/forum' directory. If you do '/' then it's the root path, meaning all pages in all directories have access to it. – Subplot
we cannot write by $_COOKIE['cookie_name'] to modify cookies, edit your answer – Analogy
B
7

Make sure there is no echo before setcookie call. setcookie communicates with browser through header, and if you called echo earlier, header+body is sent already and server cannot send setcookie to browser via header anymore. That is why you might see it is not working.

There should be a line like below in php server log file reporting warning in this case:

DEFAULT: PHP Warning:  Cannot modify header information - headers already sent by (output started at /path/to/your/script.php:YY) in /path/to/your/script.php on line XX
Brethren answered 30/3, 2019 at 19:30 Comment(0)
A
5

So while PHP will send two Set-Cookie: headers if instructed so, only the last one should persist in browsers.
The Netscape cookie spec http://curl.haxx.se/rfc/cookie_spec.html says:

Instances of the same path and name will overwrite each other, with the latest instance taking precedence. Instances of the same path but different names will add additional mappings.

However, it might be advisable to avoid such edge conditions. Restructure your application so it doesn't need to override the already sent cookie.

Applesauce answered 27/6, 2011 at 0:8 Comment(0)
T
-3

call COOKIE and delete username value SETCOOKIE("username",'',0,"/");

Translation answered 21/5, 2015 at 21:16 Comment(2)
It would be the same to just set a cookie with the new values. Instead of "deleting" the original first – Anyone
If you are still looking, I have found a solution. with ajax, you must call a new PHP page, change the value of cookies and exit, in the page already open you can read cookies with the new value. PHP can not change and read cookies on the same page. – Translation

© 2022 - 2024 β€” McMap. All rights reserved.