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