<

How to Show an Introductory Web Page -- Once Using PHP

Published on
22,619 Points
6,219 Views
9 Endorsements
Last Modified:
Awarded
Introduction
A frequent question goes something like this, "How can I show an introductory page to my clients on the first site visit, but not show it again on every visit?"  The answer is by using a cookie.  This article shows the design pattern for a home page that is aware of the need or lack of need for an introductory "splash" page.  It also allows the client to see the splash page on demand.

Using Cookies to Preserve "Stateful" Information
In HTTP, cookies are sent from the browser to the server at the time of the page request.  PHP puts these cookies into the $_COOKIE associative array.  See: http://php.net/manual/en/reserved.variables.cookies.php.  There is an important characteristic to this data flow -- the cookies you set in your PHP script are NOT put into $_COOKIE by PHP.  This means that we can test the contents of $_COOKIE to see the historical record of the cookies, and that record will not change during the execution of our script.  Cookies we set in our current script are only presented to future scripts.

The setcookie() function is used to set cookies on the client browser.  See http://www.php.net/manual/en/function.setcookie.php for the details.  Cookies are part of the HTTP headers.  This means that you may only call setcookie() before any browser output has been sent, including whitespace.  There are no exceptions to this rule.

In this script we do the following things.

(1) Unconditionally set a cookie named "splash_page" on the client browser

(2) See if the client asked to see the splash page and redirect to display the splash page if it was requested

(3) Decide whether to show the splash page (based on the cookies) or the regular page, and take the appropriate action

Another design pattern that would work about the same way would be to have the splash page put the cookies on the browser.  In either case, our tests would ask the same question, to wit, "Has the splash page been requested?" and "Does the $_COOKIE array show that the splash page has already been shown?"
 
<?php // RAY_splash_page.php


// DEMONSTRATE HOW TO SHOW A SPLASH INTRODUCTION PAGE ONCE, BUT NOT REPEATEDLY
// MAN PAGE: http://us.php.net/manual/en/function.setcookie.php
// TO SEE COOKIES IN FIREFOX, FOLLOW TOOLS => OPTIONS => PRIVACY => SHOW COOKIES


// THE COOKIE LIFE WILL CAUSE THE SPLASH PAGE TO BE SHOWN NO MORE FREQUENTLY THAN ONCE A MONTH
define('COOKIE_LIFE', 60*60*24*30); // A MONTH IN SECONDS

// CHOOSE THE COOKIE NAME AND VALUE
$cookie_name  = 'splash_page';
$cookie_value = TRUE;

// 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"]));
$y = count($x);
if ($y == 1) // MAYBE 'localhost'?
{
    $cookie_domain = $x[0];
}
else // SOMETHING LIKE 'www2.atf70.whitehouse.gov'?
{
    // USE THE LAST TWO POSITIONS TO MAKE THE HOST DOMAIN
    $cookie_domain = '.' . $x[$y-2] . '.' . $x[$y-1];
}

// MAKE THE COOKIE AVAILABLE TO HTTP, NOT JUST HTTPS
$cookie_secure    = FALSE;

// HIDE COOKIE FROM JAVASCRIPT (PHP 5.2+)
$cookie_http      = TRUE;

// SET THE COOKIE (IGNORE TESTING FOR SUCCESS)
setcookie($cookie_name, $cookie_value, $cookie_expires, $cookie_path, $cookie_domain, $cookie_secure, $cookie_http);


// AT THIS POINT, THE NEW COOKIE HAS BEEN SET, BUT IT IS _NOT_ AVAILABLE TO THIS SCRIPT.
// THE ONLY THING IN THE $_COOKIE ARRAY ARE THE OLD COOKIES, SO WE CAN TEST TO SEE IF THE
// SPLASH PAGE HAS BEEN SHOWN, AND IF NOT, WE CAN SHOW IT.  WE CAN ALSO GIVE THE CLIENT
// THE OPTION TO SEE THE SPLASH PAGE ON DEMAND BY PUTTING "intro=TRUE" INTO THE URL.


// IF THE CLIENT WANTS TO SEE THE SPLASH PAGE
if ( (isset($_GET["intro"])) && ($_GET["intro"] == 'TRUE') )
{
    header("Location: path/to/splash_page.php");
    exit;
}

// IF THE CLIENT HAS NOT SEEN THE SPLASH PAGE
if (!isset($_COOKIE["splash_page"]))
{
    header("Location: path/to/splash_page.php");
    exit;
}

// IF THE COOKIE WAS FOUND IN $_COOKIE, THE CLIENT HAS SEEN THE SPLASH PAGE
// PRODUCE THE HTML FOR THIS PAGE HERE...

Open in new window


Summary
This article shows how to detect a "first visit" to a web site.  It teaches a design that allows you to make a special welcome message to new visitors.

Further Reading
It may be helpful to understand the HTTP protocols and the workings of Client/Server architectural patterns.  For short-term storage, you can sometimes use the PHP Session.

Please give us your feedback!
If you found this article helpful, please click the "thumb's up" button below. Doing so lets the E-E community know what is valuable for E-E members and helps provide direction for future articles.  If you have questions or comments, please add them.  Thanks!
 
9
Comment
Author:Ray Paseur
[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 Comments
 
LVL 29

Expert Comment

by:rdivilbiss
"Yes" vote from me.
0
 
LVL 111

Author Comment

by:Ray Paseur
Thanks, R.  Appreciate your suggestions and assistance, ~Ray
0
 
LVL 29

Expert Comment

by:rdivilbiss
You're most welcome.
0
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.

 
LVL 51

Expert Comment

by:Mark Wills
And a big "yes" from me too...

Good article, thanks Ray...

Cheers,
Mark Wills
0
 
LVL 1

Expert Comment

by:trrsrr
'voted' for this nice article, Ray ...


Regards...
0
 
LVL 9

Expert Comment

by:Rowby Goren
I've heard about cookies but really never knew the technical side of it.

This article fills that gap!

...Rowby
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Join & Write a Comment

Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month