• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

Session First Visit Show Banner

Hello,

I would like it so that the first two page loads a visitor makes a banner will display, upon the third load it will disappear.

So, display banner twice per day per visitor.

0
movieprodw
Asked:
movieprodw
  • 6
  • 3
1 Solution
 
Ray PaseurCommented:
The session alone is not going to work for you, but a session + cookie will do the trick.  Here is the logic, as I understand your requirement:

If there is a session do not show the banner.

If no cookie, show the banner, set the session and set the cookie to value 'zero'.

If there is a cookie and the value is 'zero' set the cookie to value 'one' and show the banner.
0
 
Ray PaseurCommented:
Another way to look at this is, "show the banner on the first two page loads.  If that is what you need, then the logic would be slightly different.  If you wan tto clarify al little more, please tell me about what you want for the visitor who comes to the site, browses several pages, cloes his browser and comes back later inthe day, etc... Thanks, ~Ray
0
 
movieprodwAuthor Commented:
Hello,

I just think that the banner is a little annoying but its informative so I want it to go away after a couple of loads.

What ever is the easiest is great, so if its easier to make it so that it loads only twice on that visit or in 24 hours I do not care.

Its just one page, that searches are queried on.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Ray PaseurCommented:
OK, I'll try to give you an example.  If you use Firefox, you will be able to view and clear the cookies via the path that goes like Tools => Options => Privacy => Show Cookies

Read this example of how to set a cookie.  Back in a moment with some more specific code...
// SOME COOKIE VARIABLES
// SEE http://us3.php.net/manual/en/function.setcookie.php
 
 
// HOW LONG SHOULD THE COOKIE PERSIST?
$my_cookie_life = 60*60*24*7; // SEVEN DAYS IN SECONDS
$cookie_expires = time() + $my_cookie_life;
 
// WHAT IS THE VALUE OF THE COOKIE
$cookie_value	= $_SESSION["id"];
 
// NAME OF THE COOKIE - YOU FIND ITS VALUE WITH $_COOKIE["$cookie_name]
$cookie_name 	= "YOUR_NAME_HERE";
 
// PATH OF SLASH MEANS COOKIE IS AVAILABLE THROUGHOUT SCOPE OF YOUR SITE
$cookie_path	= '/';
 
// STARTS WITH DOT - NO WWW, ETC., FOR LARGEST SCOPE WITHIN YOUR DOMAIN
$cookie_domain	= ereg_replace('^www', '', strtolower($_SERVER["HTTP_HOST"]));
 
// MEANS SEND THE COOKIE TO http, NOT ONLY TO https
$cookie_secure	= FALSE;
$cookie_http	= TRUE;
 
// SET THE COOKIE
if (!setcookie($cookie_name, $cookie_value, $cookie_expires, $cookie_path, $cookie_domain, $cookie_secure, $cookie_http)) { /* Cookie Failed */ }

Open in new window

0
 
Ray PaseurCommented:
My apologies - please disregard that last post.  This is much better and more accurate.  You can install it and run it to see a bit about how cookies work.
<?php // RAY_cookie_example.php
 
// RECEIVE FORM INPUT AND SET A COOKIE WITH THE NAME AND VALUES FROM THE FORM
// MAN PAGE: http://us.php.net/manual/en/function.setcookie.php
// TO SEE COOKIES IN FIREFOX, FOLLOW TOOLS => OPTIONS => PRIVACY => SHOW COOKIES
 
define('COOKIE_LIFE', 60*60*24); // A 24-HOUR DAY IN SECONDS ( = 86,400 )
 
if (!empty($_POST)) // IF THE FORM HAS BEEN POSTED
{
 
// TIDY UP THE POST INPUT
   $name = substr(clean_string($_POST["name"]),0,16);
   $data = substr(clean_string($_POST["data"]),0,16);
 
// BE SURE WE HAVE USEFUL INFORMATION
   if ( ($name == '') || ($data == '') ) die("MISSING INPUT: PLEASE <a href=\"$PHP_SELF\">TRY AGAIN</a>");
 
// CONSTRUCT THE COOKIE
// USE THIS TO MAKE COOKIE EXPIRE AT END OF BROWSER LIFE
   $cookie_expires	= 0;
 
// USE THIS TO MAKE A PERSISTENT COOKIE - DEFINE COOKIE_LIFE IN SECONDS - date('Z') IS UTC OFFSET IN SECONDS
   $cookie_expires	= time() + date('Z') + COOKIE_LIFE;
 
// CHOOSE THE COOKIE NAME AND VALUE
   $cookie_name 	= $name;
   $cookie_value	= $data;
 
// MAKE THE COOKIE AVAILABLE TO ALL DIRECTORY PATHS IN THE WWW ROOT
   $cookie_path	= '/';
 
// MAKE THE COOKIE AVAILABLE TO ALL SUBDOMAINS - DOMAIN NAME STARTS WITH DOT AND OMITS WWW (OR OTHER SUBDOMAINS).
   $x = explode('.', strtolower($_SERVER["HTTP_HOST"]));
   if (!is_array($x)) // MAYBE 'localhost'?
   {
      $cookie_domain = $x;
   } else // SOMETHING LIKE 'www2.atf70.whitehouse.gov'?
   {
      $y = count($x);
      $cookie_domain = '.' . $x[$y-2] . '.' . $x[$y-1];
   }
 
// MAKE THE COOKIE AVAILABLE TO HTTP, NOT JUST HTTPS
   $cookie_secure	= FALSE;
 
// HIDE COOKIE FROM JAVASCRIPT
   $cookie_http	= TRUE;
 
// SET THE COOKIE
   if (setcookie($cookie_name, $cookie_value, $cookie_expires, $cookie_path, $cookie_domain, $cookie_secure, $cookie_http))
   {
      echo "<br/>SUCCESS!  THE COOKIE HAS BEEN SET AND WILL BE AVAILABLE TO THE NEXT PAGE LOAD \n";
   } else {
      echo "<br/>FAILURE!  THE COOKIE WAS NOT SET AS EXPECTED \n";
   }
 
// AT THIS POINT, THE COOKIE HAS BEEN SET, BUT IT IS _NOT_ AVAILABLE TO THIS SCRIPT.  IT WILL BE AVAILABLE TO THE NEXT SCRIPT!
   echo '<pre>$_COOKIE CONTAINS '; var_dump($_COOKIE); echo "</pre>\n";
   echo '<pre>$_POST CONTAINS ';   var_dump($_POST);   echo "</pre>\n";
   echo "<br/>THE COOKIE HAS BEEN SET WITH THESE VALUES: \n";
   echo "<br/>COOKIE NAME: $cookie_name \n";
   echo "<br/>COOKIE VALUE: $cookie_value \n";
   echo "<br/>COOKIE EXPIRES: $cookie_expires ";
   echo " == " . date('r') . "\n";
   echo "<br/>COOKIE PATH: $cookie_path \n";
   echo "<br/>COOKIE DOMAIN: $cookie_domain \n";
   echo "<br/>COOKIE SECURE: "; var_dump($cookie_secure); echo " \n";
   echo "<br/>COOKIE HTTP: ";   var_dump($cookie_http);   echo " \n";
 
   echo "<br/>";
   echo "<br/>TO SEE THE COOKIES, IF ANY, <a href=\"$PHP_SELF\">CLICK HERE</a> \n";
   echo "<br/>";
}
 
// END OF SETTING THE COOKIE
?>
 
 
<form method="post">
COOKIE NAME: <input name="name" /><br/>
COOKIE DATA: <input name="data" /><br/>
<input type="submit" />
</form>
 
 
<?php
// SHOW THE COOKIE ARRAY, IF ANY
echo '<pre>$_COOKIE CONTAINS '; var_dump($_COOKIE); echo "</pre>\n";
 
 
// A FUNCTION TO FORCE A STRING TO CHARACTERS ONLY
function clean_string($string)
{
   return trim(ereg_replace('[^a-zA-Z0-9_]', '', $string));
}
?>

Open in new window

0
 
movieprodwAuthor Commented:
Ok great, I will try it out!
0
 
Ray PaseurCommented:
You can install this and run it.  HTH, ~Ray
<?php // RAY_temp_movieprodw.php
error_reporting(E_ALL);
 
 
 
// SHOW A BANNER ON THE FIRST TWO ENTRIES TO THE SITE
 
 
 
// SET UP THE DATA WE WILL NEED FOR THE COOKIES
define('COOKIE_LIFE', 60*60*24); // A 24-HOUR DAY IN SECONDS ( = 86,400 )
 
// CHOOSE THE COOKIE NAME AND A DEFAULT VALUE
$cookie_name  = 'banner_counter';
$cookie_value = 'zero';
 
// USE THIS TO MAKE COOKIE EXPIRE AT END OF BROWSER LIFE
$cookie_expires	= 0;
 
// USE THIS TO MAKE A PERSISTENT COOKIE - DEFINE COOKIE_LIFE IN SECONDS - date('Z') IS UTC OFFSET IN SECONDS
$cookie_expires	= time() + date('Z') + COOKIE_LIFE;
 
// MAKE THE COOKIE AVAILABLE TO ALL DIRECTORY PATHS IN THE WWW ROOT
$cookie_path	= '/';
 
// MAKE THE COOKIE AVAILABLE TO ALL SUBDOMAINS - DOMAIN NAME STARTS WITH DOT AND OMITS WWW (OR OTHER SUBDOMAINS).
$x = explode('.', strtolower($_SERVER["HTTP_HOST"]));
if (!is_array($x)) // MAYBE 'localhost'?
{
    $cookie_domain = $x;
} else // SOMETHING LIKE 'www2.atf70.whitehouse.gov'?
{
    $y = count($x);
    $cookie_domain = '.' . $x[$y-2] . '.' . $x[$y-1];
}
 
// MAKE THE COOKIE AVAILABLE TO HTTP, NOT JUST HTTPS
$cookie_secure = FALSE;
 
// HIDE COOKIE FROM JAVASCRIPT
$cookie_http = TRUE;
 
 
 
 
// IF NO COOKIE, SET A COOKIE AND SHOW THE BANNER
if (empty($_COOKIE["$cookie_name"]))
{
    setcookie($cookie_name, $cookie_value, $cookie_expires, $cookie_path, $cookie_domain, $cookie_secure, $cookie_http);
    echo "FIRST BANNER";
 
 
// IF THE COOKIE SHOWS ONLY ONE USE
} elseif ($_COOKIE[$cookie_name] == $cookie_value)
{
    $cookie_value = 'one';
    setcookie($cookie_name, $cookie_value, $cookie_expires, $cookie_path, $cookie_domain, $cookie_secure, $cookie_http);
    echo "SECOND BANNER";
 
// IF THE BANNERS HAVE BOTH BEEN SHOWN
} else
{
    echo "NO MORE BANNERS";
}

Open in new window

0
 
movieprodwAuthor Commented:
Perfect! Thanks
0
 
Ray PaseurCommented:
Thanks for the points - it's a great question, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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