2.3. Making Information Available to All Pages in the Web Site

Passing information in a form or at the end of a URL is useful for passing information from one Web page to the next. However, in some cases, you want information to be available to every Web page in your Web site. For instance, if your user logs in to your Web site, you want every Web page to know that he or she successfully logged in. You don't want the user to have to log in again on every Web page.

You can store information that can be accessed from every Web page in your Web site with one of the following methods:

  • Storing information via cookies: You can store cookies — small amounts of information containing variable=value pairs — on the user's computer. After the cookie is stored, you can retrieve it from any Web page. However, users can refuse to accept cookies, so this method doesn't work in all environments.

  • Using PHP session functions: Beginning with PHP 4, you can use PHP functions that set up a user session and store session information on the server; this information can be accessed from any Web page. This method is useful for sessions in which you expect users to view many pages.

2.3.1. Storing information in cookies

You can store information as cookies, which are small amounts of information containing variable=value pairs, similar to the pairs you can add to a URL. The user's browser stores cookies on the user's computer. Your scripts can then use the information stored in the cookie from any Web page ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.