PHPBuilder - Sessions With PHP4

RSS Twitter

Sessions With PHP4

by: Mattias Nilsson
July 30, 2000

One of the new great features with PHP4 is the session management feature. It allows you to store variables in a session 'object' during a user visit. I used this feature recently while re-making my swedish community website ( and thought I'd share some of the experience I gained from that.

What is a Session?

First off I'd like to explain what a session is, in case you don't know what it is. A session begins when a user surfs into your website and ends when the user leaves your website (or one of your webpages terminates it explicitly). Essentially, a cookie is associated with a browser, and some kind of storage resource is allocated on the server to hold session variables. PHP4 uses files to store session variables, but one could theoretically use a database or shared memory to do the same.
All pages that uses PHP4 sessions must call the function session_start() to tell the PHP4 engine to load session related information into memory. The session_start() function tries to find the session id in the cookie field or the request parameters for the current HTTP request. If it cannot find the session id, a new session is created.

What is a Session Variable?

A session variable is a regular global variable that, when registered as a session variable, keeps its value on all pages that use PHP4 sessions. To register a session variable, assign a value to a variable that is to become a session variable and call session_register("variable_name"). On all subsequent pages that uses sessions (by calling session_start()), the variable variable_name will have the value assigned to it before it was registered as a session variable. Changes to the variable value will be automatically registered in the session and saved for further reference.

Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Mattias Nilsson



(Maximum characters: 1200). You have characters left.