1
I have a site that the style is set by the cookie "standard" or "dark"
However, if you enter it the first time it saves the cookie with "default" value, but it does not take the style.
It only works when you re-load the page.
On the first line of the header.php page you have:
<?php require('/style/css_cookie_check.php'); ?>
in the stylesheet part:
<link id="style_cor" rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/style/<?php echo $estilo_cor; ?>/style.css" type="text/css" media="screen" />
On the "css_cookie_check.php page":
<?php
global $estilo_cor;
if(!isset($_COOKIE['cor_estilo'])) {
$estilo_cor = setcookie('cor_estilo', 'padrao', (time() + (2 * 3600)));
} else {
$estilo_cor = $_COOKIE['cor_estilo'];
}
?>
Cookies cannot be used at exactly the same time they are created. They only assume after the page is reloaded. This is standard. This @Bacco tip should solve, because when it does not exist, in addition to setting the cookie, it already sets in the variable the default value (which can be used in the first access).
– Clayderson Ferreira
Clayderson Ferreira, did not know this cookie. Thanks man
– Alê Moraes
Too much man, thanks!!!
– Alê Moraes
This PHP scope business is kind of boring. I avoid using it as much as possible
global
. Global reminds me classic ASP variable scope, gives chills.– Bacco