gpt4 book ai didi

php - Cookie 造成麻烦 - 无法让 cookie 停留

转载 作者:搜寻专家 更新时间:2023-10-31 21:14:39 27 4
gpt4 key购买 nike

我遇到了 cookie 情况,我的 cookie 会存储一个颜色名称或根本不存储任何内容。所以让我们这样解释吧。我的 cookie 与我网站的外观有关,我的网站有 3 种外观:

  • 正常(完全没有 cookie)
  • 灰色(cookie 设置为“imgit_style”,值为“灰色”)
  • Inverted(cookie 设置为“imgit_style”,值为“inverted”)

我有 3 个按钮可以触发样式切换。第一个按钮用于正常,它会删除 cookie 并使外观正常。

第二个按钮用于灰色,它创建名为“imgit_style”的 cookie 并为其添加一个值 - “灰色”。

这两个可以很好地相互配合,但是当设置 Inverted cookie 时,它​​就不起作用了!当我单击第三个按钮时,它只是被删除,实际上应该用 Inverted 替换 Grey (如果已设置)或创建 cookie,如果它没有被第一个按钮设置。

希望我说得足够清楚。这是我的代码:

Styles.php

<?php
$style = '';
if (!isset($_COOKIE['imgit_style']))
{
if (isset($_POST['green']))
{
setcookie('imgit_style', '', time()-31556952);
$style = '';
}
if (isset($_POST['grey']))
{
setcookie('imgit_style', 'grey', time()+31556952);
header('Location: ' . $home_action);
}
if (isset($_POST['inverted']))
{
setcookie('imgit_style', 'inverted', time()+31556952);
header('Location: ' . $home_action);
}
}
else if (isset($_COOKIE['imgit_style']))
{
$style = '_' . $_COOKIE['imgit_style'];
if (isset($_POST['green']) && $_COOKIE['imgit_style'] == 'grey' || $_COOKIE['imgit_style'] == 'inverted')
{
setcookie('imgit_style', '', time()-31556952);
$style = '';
header('Location: ' . $home_action);
}
if (isset($_POST['grey']))
{
if ($_COOKIE['imgit_style'] == 'inverted')
{
setcookie('imgit_style', '', time()-31556952);
if (!isset($_COOKIE['imgit_style']))
{
setcookie('imgit_style', 'grey', time()+31556952);
header('Location: ' . $home_action);
}
}
}
if (isset($_POST['inverted']))
{
if ($_COOKIE['imgit_style'] == 'grey')
{
setcookie('imgit_style', '', time()-31556952);
if (!isset($_COOKIE['imgit_style']))
{
setcookie('imgit_style', 'inverted', time()+31556952);
header('Location: ' . $home_action);
}
}
}
}
?>

header.php

<!DOCTYPE HTML>
<html>
<head>
<?php include('styles.php'); ?>
<title>IMGit.org - the image host</title>

<link rel="stylesheet" type="text/css" href="css/<?php echo 'style' . $style . '.css'; ?>" media="screen" />
<link rel="icon" href="css/images/<?php echo 'favicon' . $style . '.png'; ?>" type="image/x-icon" />
<link rel="shortcut icon" href="css/images/<?php echo 'favicon' . $style . '.png'; ?>" type="image/x-icon" />
</head>

<body>
<div class="style-switcher">
<form action="" method="post">
<table>
<tr>
<td>
<span class="normal" style="vertical-align: text-top;">Switch style:</span>
<input type="submit" name="green" class="style_green-button" value="green" />
<input type="submit" name="grey" class="style_grey-button" value="grey" />
<input type="submit" name="inverted" class="style_inverted-button" value="inverted" />
</td>
</tr>
</table>
</form>
</div>

最佳答案

问题看起来是,尽管您调用 setcookie() 将 cookie 值清空 $_COOKIE 仍然保留该请求的值。在页面重新加载之前,它的内容不会改变。因此,您继续检查 !isset($_COOKIE['imgit_style']),但除非您明确取消设置,否则该值仍将被设置。

if (isset($_POST['grey']))
{
if ($_COOKIE['imgit_style'] == 'inverted')
{
// No need to unset the old one, just overwrite the new one
// setcookie('imgit_style', '', time()-31556952);
setcookie('imgit_style', 'grey', time()+31556952);
header('Location: ' . $home_action);
}
}
if (isset($_POST['inverted']))
{
if ($_COOKIE['imgit_style'] == 'grey')
{
// No need to unset the old one, just overwrite the new one
// setcookie('imgit_style', '', time()-31556952);
setcookie('imgit_style', 'inverted', time()+31556952);
header('Location: ' . $home_action);
}
}

更新

这可能是问题所在...您没有括号组来对这个 if() 语句进行逻辑分组:

if (isset($_POST['green']) && $_COOKIE['imgit_style'] == 'grey' || $_COOKIE['imgit_style'] == 'inverted')

这里发生的是任何时候 $_COOKIE['imgit_style'] == 'inverted',即使 $_POST['green'] 没有设置,随后的代码运行并删除 cookie。看起来您想将 ('grey' || 'inverted') 组合在一起:

if (isset($_POST['green']) && ($_COOKIE['imgit_style'] == 'grey' || $_COOKIE['imgit_style'] == 'inverted'))

关于php - Cookie 造成麻烦 - 无法让 cookie 停留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11709563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com