Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > SCRIPT WEB DATABASES QUICKLY WITH PHP


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Script Web databases quickly with PHP
By Craig Knudsen - 1999-09-01 Page:  1 2 3 4 5 6 7 8

Maintaining Sessions

HTTP cookies help track user sessions. For example, the first time a visitor arrives at an online store a session is created. The script then uses the session identifier any time the site needs to store information about the user, such as adding a purchase to an online order. Every request the browser makes to the server then includes the session-identifier cookie, which allows the application to identify the user associated with each request.

You can configure cookies to expire at any specified date and time or after a certain duration. Use the mktime function to generate an expiration based on a specific date or the time function to get the current date and time.

To generate a unique session ID, use the uniqid function. The result will be unique so long as no two IDs are generated at the same microsecond. The uniqid function can optionally be passed the user's IP address to remove the chance of two users getting the same session identifier.

Because cookies are sent as part of the HTTP header, generating a cookie must take place before any HTML is processed.

Listing 7: Generating a session ID that expires at the end of the session


Listing 7
 1   <?php
 2   // Generating cookies must take place before any HTML.
 3   // Check for existing "SessionId" cookie
 4   $session = $HTTP_COOKIE_VARS["SessionId"];
 5   if ( $session == "" ) {
 6     // Generate time-based unique id.
 7     // Use user's IP address to make more unique.
 8     $session = uniqid ( getenv ( "REMOTE_ADDR" ) );
 9     // Send session id - expires when browser exits
10     SetCookie ( "SessionId", $session );
11   }
12   ?>

13   <HTML>
14   <HEAD><TITLE>Session Test</TITLE></HEAD>
15   <BODY>
16   Current session id: <?php echo $session ?>
17   </BODY></HTML>

The script starts by checking for an existing session cookie. All cookies are stored in the $HTTP_COOKIE_VARS associative array. Only when there is no existing session identifier will a new one be generated.

The example in Listing 7 provides a session that lasts until the user exits the browser. Listing 8 generates a session cookie that will last until January 1, 2000.

Listing 8: Generating a session ID with a specific expiration date



Listing 8
 1   <?php
 2   // Generating cookies must take place before any HTML.
 3   // Check for existing "SessionId" cookie
 4   $session = $HTTP_COOKIE_VARS["SessionId"];
 5   if ( $session == "" ) {
 6     // Generate time-based unique id.
 7     // Use user's IP address to make more unique.
 8     $session = uniqid ( getenv ( "REMOTE_ADDR" ) );
 9     // Send session id - expires on Jan 1, 2000
10     SetCookie ( "SessionId", $session, mktime ( 0, 0, 0, 1, 1, 2000 ) );
11   }
12   ?>
13   <HTML>
14   <HEAD><TITLE>Session Test</TITLE></HEAD>

15   <BODY>
16   Current session id: <?php echo $session ?>
17   </BODY></HTML>


View Script Web databases quickly with PHP Discussion

Page:  1 2 3 4 5 6 7 8 Next Page: Rolling Up Your Sleeves

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.