Browse All Articles
> How to Show an Introductory Web Page -- Once Using PHP
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;
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;
$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') )
// IF THE CLIENT HAS NOT SEEN THE SPLASH PAGE
// IF THE COOKIE WAS FOUND IN $_COOKIE, THE CLIENT HAS SEEN THE SPLASH PAGE
// PRODUCE THE HTML FOR THIS PAGE HERE...
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.
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!