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?"
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!
Good article, thanks Ray...
This article fills that gap!