使用 PHP 更新 Cookie

发布于 2024-11-29 00:44:30 字数 999 浏览 1 评论 0原文

我正在编写一个简单的脚本来存储语言 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凹づ凸ル 2024-12-06 00:44:30

为什么要调用 setcookie() 两次?您无需删除 cookie 并重新设置。只需这一行就可以完成这项工作:

setcookie( 'language', $language, time()+3600 );

它将更新 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:

setcookie( 'language', $language, time()+3600 );

It will update the cookie's value and time.

俯瞰星空 2024-12-06 00:44:30

我认为你的代码工作正常,但 $_COOKIE 数组直到下一个请求才会更新:)

if($_REQUEST['language']) {
$language = $_REQUEST['language'];
setcookie('language', '', time()-3600);
setcookie('language', $language, time()+3600);
$_COOKIE['language'] = $language;
}

I think your code works fine, but the $_COOKIE array is not updated until the next request :)

if($_REQUEST['language']) {
$language = $_REQUEST['language'];
setcookie('language', '', time()-3600);
setcookie('language', $language, time()+3600);
$_COOKIE['language'] = $language;
}
埋情葬爱 2024-12-06 00:44:30

根据定义 $_REQUEST 中一个关联数组,默认包含 $_GET$_POST$_COOKIE 的内容。

换句话说,您的 $_GET['language']$_COOKIE['language'] 覆盖,导致 $_REQUEST['language'] > 等于$_COOKIE['语言']

如果您在代码中将 $_REQUEST 替换为 $_GET ,它将按您的预期工作:

<?php
if (isset($_GET['language'])) {
    $language = $_GET['language'];
    setcookie('language', $language, time()+3600);
    // setcookie() doesn't update $_COOKIE
    $_COOKIE['language'] = $language;
}
?>
<title>Cookie language</title>
<?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>

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:

<?php
if (isset($_GET['language'])) {
    $language = $_GET['language'];
    setcookie('language', $language, time()+3600);
    // setcookie() doesn't update $_COOKIE
    $_COOKIE['language'] = $language;
}
?>
<title>Cookie language</title>
<?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>
够运 2024-12-06 00:44:30

其中有一件事肯定无法正常工作:您在 setcookie() 之后立即调用 $_COOKIE。查看 php 文档。有写到:

一旦设置了cookie,就可以在下一页上访问它们
使用 $_COOKIE 加载

因此您只能在下一个请求时访问 cookie。 代替使用

echo $language;

There is one thing in there that definitely can't work: you are calling $_COOKIE right after setcookie(). Have a look at the php documentation. There is written:

Once the cookies have been set, they can be accessed on the next page
load with the $_COOKIE

So you will only have access to the cookie on the next request. Use

echo $language;

instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文