PHP:设置 cookies - 由于某种原因旧值没有被替换
我有一个脚本,其中设置了 2 个 cookie:
$month = time() + 60 * 60 * 24 * 30;
setcookie('id', $id, $month, '/');
setcookie('auth', $auth, $month, '/');
header('Content-Type: text/html; charset=utf-8');
print('<html><body>...etc....');
这确实工作得很好,但是:有些用户有多个 id,具体取决于他们通过 iframe 访问我的脚本的页面(社交网络)。
一个用户拥有多个 id 不是问题。但我的问题是,当我要求那个人查看他的 cookie 时,他会报告有几个名为 id 和 auth 的 cookie。我也可以自己复制它。
我实际上期望总是只有 1 个 id 和 1 个 auth cookie。
我在这里能做什么?
使用新值调用 setcookie('id', ...) 不会替换旧值吗?
谢谢你! 亚历克斯
I have a script where I set 2 cookies:
$month = time() + 60 * 60 * 24 * 30;
setcookie('id', $id, $month, '/');
setcookie('auth', $auth, $month, '/');
header('Content-Type: text/html; charset=utf-8');
print('<html><body>...etc....');
This does work well, but: some users have several id's, depending on from which page (social network) they access my script through an iframe.
A user having several id's is not a problem. But my problem is that when I ask that person to look at his cookies, he'll report that there are several cookies called id and auth. And I can reproduce it myself too.
And I was actually expecting there always to be just 1 id and 1 auth cookie.
What can I do here?
Doesn't calling setcookie('id', ...) with a new value replace the old value?
Thank you!
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cookie 基于 KV 方案(Key=Value)概念,并且 Key 充当唯一标识符。
setcookie
的三个主要效应器是setcookie("id","value") 创建 cookie
$_COOKIE["id"]
读取 cookiesetcookie("id","new value") 更新 Cookie
setcookie("TestCookie", "", time() - 10);
所以是的,你的问题是正确的,你应该看看其他可能阻止 cookie 状态的因素。
Cookies are based on a KV Scheme (Key=Value) concept and the Key's act as unique identifiers.
The three primary effectors of
setcookie
aresetcookie("id","value")
$_COOKIE["id"]
setcookie("id","new value")
setcookie ("TestCookie", "", time() - 10);
so yes your correct in your question, you should have a look at other factors that may deter the cookie states.