饼干造成麻烦 - 不能让饼干停留

时间:2012-07-29 13:20:08

标签: php post cookies

我有一个cookie情况,我的cookie会存储颜色名称或根本没有。所以我们这样解释吧。我的cookies与我网站的外观有关,我的网站有3个外观:

  • 正常(完全没有cookie)
  • 灰色(将Cookie设置为'imgit_style',值为“灰色”)
  • 倒置(将Cookie设置为'imgit_style',值为“倒置”)

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

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

这两个相互之间完全没问题,但是当设置倒置 cookie时它不会起作用!当我点击第三个按钮时,它会被删除,该按钮实际应该用反转替换灰色(如果已设置)或者如果第一个按钮未设置则创建cookie。 / p>

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

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>

2 个答案:

答案 0 :(得分:1)

问题似乎是,尽管您将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'],后续代码也会运行并删除co​​okie。看起来您想要将('grey' || 'inverted')组合在一起:

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

答案 1 :(得分:1)

您不需要使用长条件,因为它会造成不必要的处理并使代码不可测试。

请考虑以下代码:

if (isset($_POST['green']))
{
    setcookie('imgit_style', '', time() + 3600);
    $style = 'green';
}
if (isset($_POST['grey']))
{
    setcookie('imgit_style', 'grey', time() + 3600);
    $style = 'grey';
}
if (isset($_POST['inverted']))
{
    setcookie('imgit_style', 'inverted', time() + 3600);
    $style = 'inverted';
}

根据设置按钮,它将相应地设置cookie。将Cookie设置为''将在用户端删除它,您可以使用print_r($_COOKIES);获取此信息,但仅在重新加载页面后才能获取。

当然,要检查green设置,您需要使用isset($_COOKIES['imgit_style']),因为在这种情况下,Cookie将被取消设置。

使用上述方法,以下只是附录:

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

这有点狡猾,因为你使用OR ||AND &&没有分组的范例,让自己受到operator preference的统治。您应该根据您的目标对条件进行分组。

例如,如果您希望在green设置为POST但Cookie当前为greyinverted时让条件阻止运行,则需要使用{{1} }。据我所知,这是每个布尔代数的默认行为:

  • 首先我们检查是否设置了if ( (isset($_POST['green'])) && ($_COOKIE['imgit_style'] == 'grey' || $_COOKIE['imgit_style'] == 'inverted') )
    • 如果$_POST['green'],则整个条件都不可能成立,所以这是退出点。
    • 如果FALSE,我们会检查Cookie是TRUE还是grey
      • 如果其中任何一个是inverted,则等式将为TRUE并且条件分支会运行。
      • 如果两者TRUE,则公式为FALSE,为我们提供退出点。