?
Solved

Session First Visit Show Banner

Posted on 2009-05-15
9
Medium Priority
?
250 Views
Last Modified: 2013-12-12
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
Comment
Question by:movieprodw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24398947
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24398964
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
 
LVL 1

Author Comment

by:movieprodw
ID: 24399291
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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24399335
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24399370
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
 
LVL 1

Author Comment

by:movieprodw
ID: 24399441
Ok great, I will try it out!
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 24399488
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
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 31582072
Perfect! Thanks
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24459269
Thanks for the points - it's a great question, ~Ray
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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