你如何在 PHP 中更新 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6487564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:32:40  来源:igfitidea点击:

How do you update a cookie in PHP?

phpcookieshttpcookiecakephp

提问by Cookie

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

如果我setcookie()使用相同的 cookie 名称调用两次,则会创建两个 cookie。

How do you update an existing cookie?

如何更新现有的 cookie?

采纳答案by Francisc

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

您不能更新 cookie 本身,但是您可以覆盖它。否则,这就是您要查找的内容: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.

您也可以使用超级全局$_COOKIE['cookie_name']来读取/写入 cookie。

回答by Omar Alahmed

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

您可以使用 setcookie() 函数更新 cookie 值,但您应该在第四个参数中添加 '/',即 'path' 参数,以防止创建另一个具有相同名称的 cookie。

i.e. setcookie('cookie_name', 'cookie_value', $exp_date, '/');

IE setcookie('cookie_name', 'cookie_value', $exp_date, '/');

回答by mario

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.htmlsays:

因此,虽然 PHP 会发送两个 Set-Cookie: 标头(如果有指示),但只有最后一个应该在浏览器中保持不变。
Netscape cookie 规范http://curl.haxx.se/rfc/cookie_spec.html说:

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.

然而,避免这种边缘条件可能是可取的。重构您的应用程序,使其不需要覆盖已发送的 cookie。

回答by lashgar

Make sure there is no echobefore setcookiecall. setcookiecommunicates with browser through header, and if you called echoearlier, header+body is sent already and server cannot send setcookieto browser via header anymore. That is why you might see it is not working.

确保没有调用echosetcookiesetcookie通过 header 与浏览器通信,如果您echo之前调用,header+body 已经发送,服务器无法再setcookie通过 header发送到浏览器。这就是为什么您可能会看到它不起作用的原因。

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

在这种情况下,php 服务器日志文件报告警告中应该有如下一行:

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

回答by jalal-dev

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

调用 COOKIE 并删除用户名值 SETCOOKIE("username",'',0,"/");