I'm having a cookie situation, where my cookies would store a color name or nothing at all. So let's explain it this way. My cookies are related to the look of my website, my website has 3 looks:
I have 3 buttons which trigger the styles to switch. The first button is for Normal, it deletes the cookie and makes the look normal.
The second button is for Grey, it creates the cookie with name 'imgit_style' and adds a value to it - "grey".
These two work completely fine with each other, but when setting the Inverted cookie it wont work! It just gets deleted when I click the third button which should actually replace Grey (if it's set) with Inverted OR create the cookie if it was unset by the first button.
Hope I was clear enough. Here is my code:
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>
The problem looks to be that although you call setcookie() blank out the cookie value $_COOKIE still holds that value for this request. Its contents would not change until the page is reloaded. So you go on to check !isset($_COOKIE['imgit_style']), but that value will still be set unless you explicitly unset it.
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);
}
}
This may be the issue... You have no parenthetical groups to logically group this if() statement:
if (isset($_POST['green']) && $_COOKIE['imgit_style'] == 'grey' || $_COOKIE['imgit_style'] == 'inverted')
What happens here is that anytime $_COOKIE['imgit_style'] == 'inverted', even if $_POST['green'] isn't set, the subsequent code runs and removes the cookie. Looks like you want instead to group the ('grey' || 'inverted') together:
if (isset($_POST['green']) && ($_COOKIE['imgit_style'] == 'grey' || $_COOKIE['imgit_style'] == 'inverted'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With