How do you update a cookie in PHP?

Viewed 96500

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

How do you update an existing cookie?

5 Answers

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
Related