Jump to content

Having Trouble Validating Cookie


phpQuestioner

Recommended Posts

I have a script that I created that lets user set the default layout version on a website that I created. So I thought it would be cool; if when the end user choose the layout; the browser would automatically redirect them back to their preferred layout. This way they do not have select their preferred layout each time they view the website. So I accomplished this by setting a cookie for 10 years and if the cookie was set; the end user would automatically be redirected to layout v2.0 or whatever. Well then I decided to have this page check for the cookie and if it was set; the cookie was not to set again. Well this is where the problem is now occuring. Even if the cookie is set; the script has the browser to set up the same cookie again, but obviously with a different expiration time. I was wondering it someone could look at this and tell me where I am going wrong.

 

I tried to check for this cookie with ISSET and EMPTY global constants and nether one worked by them self; the cookie was still set. So now I have tried to use them both together; but the cookie is still being set, even if it already exist.

 

Can someone have a look see and tell me what this issue is?

 

Here Is My Code:

 

<?php

// 10 Year Cookie Timer (Includes Leap Year Calculations)
$lastuntil = 60*60*24*365.30*10;

// User Designated Page Layout Cookie/Cache Will Return To Their Specifically Chosen Layout For 10 Years
if ($layout == "v1") {
setcookie("expanded","",time()-$lastuntil);
}
else if (isset($_COOKIE['expanded'])) {
$layout="v2";
}
?>
<html>
<head>
<title></title>
</head>
<body>

<?php

if ($layout == "v1")
{
setcookie("expanded","layout",time()-$lastuntil);
setcookie("normal","layout",time()+$lastuntil);
// display layout 1
}

else if ($layout == "v2")
{
if ((!isset($_COOKIE['expanded'])) || (empty($_COOKIE['expanded'])))
  {
   setcookie("normal","layout",time()-$lastuntil);
   setcookie("expanded","layout",time()+$lastuntil);
  }
// display layout 2
else 
{
// display layout 1
}

?>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/77795-having-trouble-validating-cookie/
Share on other sites

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.