29

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

How do you update an existing cookie?

smottt
  • 3,091
  • 11
  • 37
  • 42
Cookie
  • 301
  • 1
  • 3
  • 4

5 Answers5

37

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 */
Omar Alahmed
  • 702
  • 7
  • 8
23

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.

Danogentili
  • 477
  • 5
  • 11
Francisc
  • 66,160
  • 57
  • 172
  • 264
  • 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 :| – Cookie Jun 27 '11 at 00:06
  • 1
    Are they from the same domain? www or no-www counts. – Francisc Jun 27 '11 at 00:13
  • hmm I didn't set the domain argument.. But the cookies are set from the pages of one domain – Cookie Jun 27 '11 at 00:14
  • You don't need to specifically set the domain if they are from the same domain. – Francisc Jun 27 '11 at 00:16
  • It's worth always setting the path param though, that might be the issue here – Tim Fountain Jun 27 '11 at 00:18
  • ok it works if I add the path argument (I added `/`), I dont understand what's that for?? – Cookie Jun 27 '11 at 00:23
  • 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. – Klinky Jun 27 '11 at 03:39
  • 3
    we cannot write by $_COOKIE['cookie_name'] to modify cookies, edit your answer – Amir Fo Aug 29 '17 at 14:06
6

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
lashgar
  • 4,434
  • 3
  • 30
  • 42
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.

mario
  • 138,064
  • 18
  • 223
  • 277
-2

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

jalal-dev
  • 1
  • 1
  • It would be the same to just set a cookie with the new values. Instead of "deleting" the original first – mowgli Apr 13 '19 at 19:34
  • 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. – jalal-dev Oct 18 '19 at 15:31