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

x
?
Solved

php cookie question

Posted on 2011-04-29
5
Medium Priority
?
374 Views
Last Modified: 2012-05-11
I have a banner, I want to show it to my user only once a day.

I think cookies the best way for this. ( I use PHP )

I need someting like
if the user has already seen it print nothing
else print the source code of the banner..
what do you suggest? I have try the below code but it didn't work.

setcookie("cookie[one]", "cookieone", time()+86400, '/' ); // one day is 86400 sec.

if ($_COOKIE["cookieone"] = "cookieone")
{
      echo "do not show the banner";
} else {
      echo "show the banner";
}
0
Comment
Question by:Braveheartli
5 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35489786
See what the COOKIE holds

<?php

setcookie("cookie[one]", "cookieone", time()+86400, '/' ); // one day is 86400 sec.

print_r($_COOKIE);

if (isset($_COOKIE["cookie"]))
{
      echo "do not show the banner";
} else {
      echo "show the banner";
}

?>

Open in new window

0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 800 total points
ID: 35489806
if (isset($_COOKIE["cookie"]["one"]))
{
      echo "do not show the banner";
} else {
      echo "show the banner";
}
0
 
LVL 8

Assisted Solution

by:shanikawm
shanikawm earned 800 total points
ID: 35489852
Try this, Refresh the page and see.
<?php
if(!isset($_COOKIE['counts']))
{
        $_COOKIE['counts'] = 0;
}
$counts = $_COOKIE['counts'] + 1;
setcookie('counts',$counts,time()+86400);
?>
<html>
<head>
<title> Test </title>
</head>
<body>
<?php
if ($counts > 1)
{
  echo("Your $counts visit to this page");
}
else
{
  echo('Your first visit to this page');
}
?>
</body>
</html>

Open in new window

0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 total points
ID: 35489856
I think the check should use the same name as the set.

if ($_COOKIE["cookie"]["one"] = "cookieone")

However there is a comment on this page http://us3.php.net/manual/en/function.setcookie.php that says that won't work.

If you make it:

setcookie("cookieone", "cookieone", time()+86400, '/' ); // one day is 86400 sec.

It should work.
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 35489900
thank you very much
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question