PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

Session Tracking: Part 2

by W.J. Gilmore
05/10/2001

As you learned in the last article, session tracking can add a whole new dimension to your web site. This week, I'll build upon what we learned, and show you how to create custom functions to store user session data. However, before delving further into how these functions are created, let's take a moment to ask why we would want to do this in the first place.

Why would it be necessary to create custom functions instead of using the default file method offered by PHP? Among other things, it facilitates the use of a database for storing session data. Database storage of session data is useful not only for reasons of efficiency, but also because it allows session information to be easily retrieved across different networks and domains.

Configuration

If you remember from the last article, session information is stored by default within separate files -- one file for each session identification (SID) number. There are also two other methods in which session information can be stored: shared memory and via creation of the custom storage functions. I'll discuss configuration of the latter method in this section.

Open your php.ini file and locate the directive:

session.save_handler = files ; handler used to store/retrieve data

To make use of the custom storage functions that I'll create later on in this article, session.save_handler should be set as follows:

session.save_handler = user ; handler used to store/retrieve data

This tells the PHP engine that you intend to define your own session storage functions. We'll get to exactly how this is accomplished later.

The functions

Regardless of what storage media you intend to use, there are six functions that must be defined. These six functions perform the operations required by the PHP engine to carry out its session-handling functionality. I'll define each function's purpose and parameters here:

session_open($session_save_path, $session_name)
The function session_open() simply initializes any elements that may be used throughout the sessions process. The two input parameters $session_save_path and $session_name refer to the configuration directives found in the php.ini. We'll use PHP's get_cfg_var() function to retrieve these configuration values in later examples.

session_close()
The function session_close() operates much like a typical handler function does, closing any open resources initialized by session_open(). As you can see, there are no input parameters for this function. Keep in mind that this does not destroy the session. That is the job of session_destroy(), introduced later in this section.

session_read($sessionID)
This function reads the session data from the storage media. The input parameter $sessionID refers to the session ID that will be used to identify the data stored for this particular client.

session_write($sessionID, $value)
This function writes the session data to the storage media. The input parameter $sessionID is the variable name, and the input parameter $value is the session data.

session_destroy($SID)
This function is likely the last function you'll call in your script. It will destroy the session and all relevant session variables. The input parameter $SID refers to the session ID in the currently open session.

session_garbage_collect($lifetime)
This function effectively deletes all sessions that have expired. The input parameter $lifetime refers to the session configuration directive session.gc_maxlifetime, found in the php.ini file.

Pages: 1, 2, 3

Next Pagearrow




Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com