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

AID: 3314
  • Status: Published

9315 points

  • ByRay_Paseur
  • TypeTutorial
  • Posted on2010-06-24 at 12:30:32
Awards
  • Community Pick
  • Experts Exchange Approved

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.

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...

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:

Select allOpen in new window

Asked On
2010-06-24 at 12:30:32ID3314
Tags

HTTP

,

PHP

,

Cookies

Topic

PHP Scripting Language

Views
1467

Comments

Expert Comment

by: rdivilbiss on 2010-06-27 at 14:13:54ID: 16214

"Yes" vote from me.

Author Comment

by: Ray_Paseur on 2010-06-27 at 17:17:25ID: 16218

Thanks, R.  Appreciate your suggestions and assistance, ~Ray

Expert Comment

by: rdivilbiss on 2010-06-27 at 17:41:40ID: 16221

You're most welcome.

Expert Comment

by: mark_wills on 2010-06-27 at 18:59:53ID: 16222

And a big "yes" from me too...

Good article, thanks Ray...

Cheers,
Mark Wills

Expert Comment

by: trrsrr on 2010-09-04 at 22:14:11ID: 19100

'voted' for this nice article, Ray ...


Regards...

Add your Comment

Please Sign up or Log in to comment on this article.

Loading Advertisement...

Top PHP Experts

  1. Ray_Paseur

    326,882

    Wizard

    4,070 points yesterday

    Profile
    Rank: Savant
  2. Roads_Roads

    77,834

    Master

    0 points yesterday

    Profile
    Rank: Genius
  3. maeltar

    71,332

    Master

    0 points yesterday

    Profile
    Rank: Guru
  4. StingRaY

    70,054

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  5. DaveBaldwin

    64,155

    Master

    664 points yesterday

    Profile
    Rank: Genius
  6. jason1178

    37,050

    0 points yesterday

    Profile
    Rank: Genius
  7. COBOLdinosaur

    30,996

    664 points yesterday

    Profile
    Rank: Genius
  8. xterm

    28,850

    0 points yesterday

    Profile
    Rank: Sage
  9. eriksmtka

    27,641

    0 points yesterday

    Profile
    Rank: Master
  10. smadeira

    26,150

    0 points yesterday

    Profile
    Rank: Guru
  11. webmatrixpune

    23,436

    0 points yesterday

    Profile
    Rank: Guru
  12. logudotcom

    19,598

    10 points yesterday

    Profile
    Rank: Genius
  13. bportlock

    17,470

    0 points yesterday

    Profile
    Rank: Genius
  14. Derokorian

    17,368

    0 points yesterday

    Profile
    Rank: Guru
  15. maestropsm

    16,698

    0 points yesterday

    Profile
    Rank: Master
  16. leakim971

    16,600

    0 points yesterday

    Profile
    Rank: Genius
  17. alex_code

    16,402

    0 points yesterday

    Profile
    Rank: Guru
  18. mwvisa1

    14,400

    0 points yesterday

    Profile
    Rank: Genius
  19. hernst42

    14,332

    0 points yesterday

    Profile
    Rank: Genius
  20. pratima_mcs

    14,200

    0 points yesterday

    Profile
    Rank: Genius
  21. Slick812

    13,900

    0 points yesterday

    Profile
    Rank: Sage
  22. elvin66

    12,628

    0 points yesterday

    Profile
    Rank: Wizard
  23. zappafan2k2

    12,200

    0 points yesterday

    Profile
    Rank: Guru
  24. TerryAtOpus

    11,600

    0 points yesterday

    Profile
    Rank: Genius
  25. amar_bardoliwala

    11,500

    0 points yesterday

    Profile
    Rank: Master

Hall Of Fame