PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY What (Photoshop web design)

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY What this chapter covers: Understanding sessions Creating a file-based login system Setting a time limit for sessions Using sessions to keep track of information The Web is a brilliant illusion. When you visit a well-designed website, you get a great feeling of continuity, as though flipping through the pages of a book or a magazine. Everything fits together as a coherent entity. The reality is quite different. Each part of an individual page is stored and handled separately by the web server. Apart from needing to know where to send the relevant files, the server has no interest in who you are. Each time a PHP script runs, the variables exist only in the server s memory and are normally discarded as soon as the script finishes. Even variables in the $_POST and $_GET arrays have only a brief life span. Their value is passed once to the next script and then removed from memory unless you do something with it, such as store the information in a hidden form field. Even then, it persists only if the form is submitted. To get around these problems, PHP uses sessions. After briefly describing how sessions work, I ll show you how you can use session variables to create a simple file-based login system and pass information from one page to another without the need to use hidden form fields. What sessions are and how they work A session ensures continuity by storing a random identifier on the web server and on the visitor s computer (as a cookie). The web server uses the cookie to recognize that it s communicating with the same person (or, to be more precise, with the same computer). Figures 9-1 and 9-2 show the details of a simple session created in my local testing environment. As you can see from the left screenshot in Figure 9-1, the cookie stored in the browser is called PHPSESSID, and the content is a jumble of letters and numbers (it s actually a 32-digit hexadecimal number). A matching file, which contains the same jumble of letters and numbers as part of its filename, is created on the web server (shown on the right). Figure 9-1. PHP sessions store a unique identifier as a cookie in the browser (left) and on the server (right).
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Leave a Reply