3

私はクッキーの状況にあり、私のクッキーは色の名前を保存するか、まったく保存しません。それでは、このように説明しましょう。私のクッキーは私のウェブサイトの外観に関連しています。私のウェブサイトには3つの外観があります。

  • 通常(Cookieなし)
  • 灰色(Cookieが「imgit_style」に設定され、値が「灰色」の場合)
  • 反転(Cookieが「imgit_style」に設定され、値が「inverted」)

スタイルを切り替えるための3つのボタンがあります。最初のボタンは通常用で、Cookieを削除し、通常の外観にします。

2番目のボタンはGray用で、「imgit_style」という名前のCookieを作成し、それに値「grey」を追加します。

これら2つは互いに完全に正常に機能しますが、反転Cookieを設定すると機能しません。3番目のボタンをクリックすると削除されます。これは実際にGray(設定されている場合)をInvertedに置き換えるか、最初のボタンで設定されていない場合は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>
4

2 に答える 2

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']、後続のコードが実行されてCookieが削除されます。代わりにグループ化する必要があるようです('grey' || 'inverted')

if (isset($_POST['green']) && ($_COOKIE['imgit_style'] == 'grey' || $_COOKIE['imgit_style'] == 'inverted'))
于 2012-07-29T13:41:42.703 に答える
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をに設定する''と、ユーザー側でCookieが削除されます。これを取得するには、を使用しますがprint_r($_COOKIES);、ページを再読み込みした後のみです。

もちろん、設定を確認するには、Cookieが設定解除されるため、greenを使用する必要があります。isset($_COOKIES['imgit_style'])

上記の方法を使用することにより、以下は単なる付録です。

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

OR ||これは、グループ化されていない括弧を使用しているため、少し危険です。これにより、演算子の優先順位AND &&が支配的になります。やりたいことに基づいて条件をグループ化する必要があります。

たとえば、greenPOSTに設定されているが、Cookieが現在greyまたはinvertedである場合に条件ブロックを実行させたい場合は、を使用する必要がありますif ( (isset($_POST['green'])) && ($_COOKIE['imgit_style'] == 'grey' || $_COOKIE['imgit_style'] == 'inverted') )。私の知る限り、これはブール代数ごとのデフォルトの動作です。

  • まず、$_POST['green']が設定されているかどうかを確認します。
    • の場合FALSE、条件全体が真になる可能性はないため、これは出口点です。
    • の場合TRUE、CookieがgreyORであるかどうかを確認しinvertedます。
      • それらのいずれかがである場合TRUE、方程式はとなりTRUE、条件分岐が実行されます。
      • 両方がである場合FALSE、方程式はになりFALSE、出口点が得られます。
于 2012-07-29T14:24:31.537 に答える