How do you update a cookie in PHP?

2019-02-04 05:06发布

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

How do you update an existing cookie?

4条回答
Rolldiameter
2楼-- · 2019-02-04 05:38

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.

查看更多
虎瘦雄心在
3楼-- · 2019-02-04 06:01

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

查看更多
对你真心纯属浪费
4楼-- · 2019-02-04 06:02

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', $exp_date, '/');

查看更多
The star\"
5楼-- · 2019-02-04 06:04

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/write cookies.

查看更多
登录 后发表回答