使用 PHP 更新 Cookie
我正在编写一个简单的脚本来存储语言 cookie。
当用户选择一种语言时,我希望 cookie 能够相应地更新。
由于某种原因,这不起作用——一旦创建 cookie,它就不会更新。
这是代码:
<?php
if($_REQUEST['language']) {
$language = $_REQUEST['language'];
setcookie('language', '', time()-3600);
setcookie('language', $language, time()+3600);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cookie language</title>
</head>
<body>
<?php
echo $_COOKIE['language'];
?>
<ul>
<li><a href="delete.php?language=en">en</a></li>
<li><a href="delete.php?language=de">de</a></li>
<li><a href="delete.php?language=es">es</a></li>
</ul>
</body>
</html>
非常感谢任何帮助!
谢谢。
I'm writing a simple script to store a language cookie.
When a user selects a language, I'd like the cookie to be updated accordingly.
For some reason this doesn't work - once the cookie is created, it won't update.
Here's the code:
<?php
if($_REQUEST['language']) {
$language = $_REQUEST['language'];
setcookie('language', '', time()-3600);
setcookie('language', $language, time()+3600);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cookie language</title>
</head>
<body>
<?php
echo $_COOKIE['language'];
?>
<ul>
<li><a href="delete.php?language=en">en</a></li>
<li><a href="delete.php?language=de">de</a></li>
<li><a href="delete.php?language=es">es</a></li>
</ul>
</body>
</html>
Any help much appreciated!
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么要调用
setcookie()
两次?您无需删除 cookie 并重新设置。只需这一行就可以完成这项工作:它将更新 cookie 的值和时间。
Why do you call
setcookie()
twice? You don't need to delete the cookie and set it again. Just this line will do the job:It will update the cookie's value and time.
我认为你的代码工作正常,但 $_COOKIE 数组直到下一个请求才会更新:)
I think your code works fine, but the $_COOKIE array is not updated until the next request :)
根据定义
$_REQUEST
中一个关联数组,默认包含$_GET
、$_POST
和$_COOKIE
的内容。换句话说,您的
$_GET['language']
被$_COOKIE['language']
覆盖,导致$_REQUEST['language']
> 等于$_COOKIE['语言']
。如果您在代码中将
$_REQUEST
替换为$_GET
,它将按您的预期工作:As per definition
$_REQUEST
in an associative array that by default contains the contents of$_GET
,$_POST
and$_COOKIE
.In other words your
$_GET['language']
gets overwritten by$_COOKIE['language']
resulting in$_REQUEST['language']
equal to$_COOKIE['language']
.If you replace
$_REQUEST
with$_GET
in your code it will work as you expect:其中有一件事肯定无法正常工作:您在
setcookie()
之后立即调用$_COOKIE
。查看 php 文档。有写到:因此您只能在下一个请求时访问 cookie。 代替使用
。
There is one thing in there that definitely can't work: you are calling
$_COOKIE
right aftersetcookie()
. Have a look at the php documentation. There is written:So you will only have access to the cookie on the next request. Use
instead.