Help with PHP Cookie for Template Switching

Ok I am having a problem with this code I made, it seems that whenever I try to switch from the night template to the gloss, it requires 2 reloads for the change to take effect. The redirect function just simply redirects using php, I know there is no problem with it. Please help.

I am sorry about the messy code, I am a real beginner so please help.

Here is the code that appears right before the code of the night template:
[php]

<? //Redirect Fuction appears before this $cf = "night"; if($t == gloss) { $cf = "gloss"; // clear cookie setCookie("mscookietemplate", "", time() -60); // set cookie setCookie("mscookietemplate","gloss", time() + 3600); } if($t == night || !isset($t)) { $cf = "night"; // clear cookie setCookie("mscookietemplate", "", time() -60); // set cookie setCookie("mscookietemplate","night", time() + 3600); } if($mscookietemplate == "night")$cf = "night"; if($mscookietemplate == "gloss")$cf = "gloss"; //End Cookie if ($t == night || $_COOKIE["mscookietemplate"] != gloss || $_COOKIE["mscookietemplate"] == night || !isset($_COOKIE["mscookietemplate"])) { if(!isset($t))redirect("http://www.mysite.com/index.php?t=night") ?>

------------------------------Here is where the night template code is

------------------------------Here is the drop down menu that switches the templates Choose a Design Night Design (Dark) Gloss Design (Bright) ------------------------------This is where the night template code ends and the gloss template starts <? } else if ($t == gloss || $_COOKIE["mscookietemplate"] == gloss) { if (!isset($t))redirect("http://www.mysite.com/index.php?t=gloss")?> ------------------------------Here is the drop down menu that switches the templates Choose a Design Night Design (Dark) Gloss Design (Bright) ------------------------------This is where the gloss template ends <? } ?> [/php]

You know that cookies are read first, and then set, right? So a page refresh is in order (I can’t see it, but I presume you use the header() function, before exiting the code?). Secondly, your code is messy, especially the if statements that determine which template to use. I have a feeling you didn’t plot things out on the requirements for each. For example: your else if () statement should become an else {} statement, since the templates are mutually exclusive. Your if () statement’s requirements should determine when to switch to which template:

[php]
// Set cookies code here

// Refresh page code here (but ONLY when a cookie has been (re)set!)

$myCookie = “gloss”;
if ($isset($_COOKIE[‘myCookie’] && in_array($_COOKIE[‘myCookie’], array(“gloss”, “night”)))) {
$myCookie = $_COOKIE[‘myCookie’];
}

if ($myCookie == “gloss”) {
// display gloss header
} else {
// display night header
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service