[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

php cookie question

I want to show the banner once in a day to my users.
I have set a cookie
if the user has visited my page whitin a day I want to redirect it only once to the page which has the banner.

I wrote the below code, but it worked only once, although it has been more than one day, redirection doesn’t work.


if(!isset($_COOKIE['countssss']))
{
        $_COOKIE['countssss'] = 0;
}
$countssss = $_COOKIE['countssss'] + 1;
setcookie('countssss',$countssss,time()+60*60*24); //24 hours

if ($countssss <= 1)
{
    header("Location: http://mywebsite/mypage.php");
    exit;
}
0
Braveheartli
Asked:
Braveheartli
  • 3
  • 2
3 Solutions
 
m4trixCommented:
Once the cookie is set, every time you refresh the page, another 24 hours is tacked onto the cookie. Your code would work if someone visited the page at 10am today, and then visited at 11am tomorrow. However, if someone visits the page at 10am today, and 9am tomorrow, then they'd have to wait until 9:01am on the day after THAT for it to redirect again.

So, you need to only setcookie if there is no cookie already. Maybe this:
if(!isset($_COOKIE['countssss']))
{
        $_COOKIE['countssss'] = 0;
        setcookie('countssss',1,time()+60*60*24); //24 hours
}
$countssss = $_COOKIE['countssss'] + 1;

if ($countssss <= 1)
{
    header("Location: http://mywebsite/mypage.php");
    exit;
}

Open in new window

0
 
BraveheartliMarketingAuthor Commented:
Dear m4trix,
thank you,

you are right. I will let you know.
0
 
m4trixCommented:
Oh, if you are using $countssss for any other reason (say to show the # of times that the user has visited the page in the last 24 hours) and need to maintain that functionality, then you can actually create a second cookie that tracks the expiry of the countssss cookie. The following seems to work perfectly for me:
if(!isset($_COOKIE['countssss']))
{
        $_COOKIE['countssss'] = 0;
        $setExpiry = time()+60*60*24;        //24 hours from now
        setcookie('ssssexpire',$setExpiry);  //When 'countssss' expires
}
$countssss = $_COOKIE['countssss'] + 1;

// Update 'countssss' with the new visit, and set the expiry. Note that if "setExpiry" exists,
// it uses that. Otherwise the user will be redirected twice when the cookie expires
setcookie('countssss',$countssss,$setExpiry?$setExpiry:$_COOKIE['ssssexpire']);

if ($countssss <= 1)
{
    header("Location: http://mywebsite/mypage.php");
    exit;
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
This is a common question and there is a well-understood design pattern that we use.  This article describes the design pattern.  It contains a code sample you can copy and modify for your needs.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3314-How-to-Show-an-Introductory-Web-Page-Once-Using-PHP.html

HTH, ~Ray
0
 
m4trixCommented:
Oh, one last thing, if you DON'T care about keeping track of # of visits, then you can make it really simple:
if(!isset($_COOKIE['countssss']))
{
        setcookie('countssss',1,time()+60*60*24);  //24 hours from now
        header("Location: http://mywebsite/mypage.php");
        exit;
}

Open in new window

0
 
BraveheartliMarketingAuthor Commented:
thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now