Jump to content

[SOLVED] Having Problem With Cookies For Viewer Chosen Layout.


phpQuestioner

Recommended Posts

I am trying to create a user interaction experience on a web page that lets them choose there layout, but I do not want it to be just CSS based; I want it be PHP based. I am having trouble setting and erasing my cookies to make this work.  I pretty sure the reason is because the code that checks for the cookie is in the top of the page and it is stopping the execution of the other if else condition. The thing is, that I am not sure exactly were to put this code at to make it work like I want it to. I tried to nest the if else condition, that checks for the cookie in the if else condition that checks for the layout, but that did not help. Can someone give my code below a once over and tell me what would be the best we to go about doing this.

 

 

<?php

if (isset($_COOKIE['expanded'])) {
setcookie("normal","",time()-3600);
$layout="v2";
}
else if (isset($_COOKIE['normal'])) {
setcookie("expanded","",time()-3600);
$layout="v1";
}
else {
setcookie("expanded","",time()-3600);
setcookie("normal","",time()-3600);
$layout="";
}



if ($layout == "v2") {
setcookie("expanded","layout",time()+3600);
// display expanded layout here
}
else if ($layout == "v1") {
setcookie("normal","layout",time()+3600);
// display normal layout here
}
else {
// display normal layout here
}

?>

OK - I figured out how to do this. I decided to only check for the expanded version cookie, but I set a normal version cookie, just as a back up (probably really did not need that - I know). This did accomplish what I wanted it to do though; so maybe this will help some else out who is trying to create a php version of a user defined layout switcher - any way, I hope it does.

 

 

<?php

// User Designated Page Layout Cookie Will Return To Their Specifically Chosen Layout For 90 Days

if ($layout == "v1") {
setcookie("expanded","",time()-7776000);
}
else if (isset($_COOKIE['expanded'])) {
$layout="v2";
}

?>
<head>

  <title>Untitled</title>

</head>
<body>

<?php

if ($layout == "v1")
{
setcookie("normal","layout",time()+7776000);
// display layout version 1 contents
}
else if ($layout == "v2")
{
setcookie("normal","layout",time()-7776000);
setcookie("expanded","layout",time()+7776000);
// display layout version 2 contents
}
else {
// display layout version 1 contents
}

?>
</body>

</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.