Source for file session.php
Documentation is available at session.php
* File containing the Session class
* @version $Id: session.php 248 2006-08-23 06:24:54Z john $
* @copyright (c) 2005 John Peterson
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* Keep track of state of the client's session with the server
* Since there is no continuous connection between the client and the
* web server, there must be some way to carry information forward
* from one page to the next. PHP does this with a global array variable
* {@link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.session $_SESSION}
* which is automatically restored from an area of the server's hard disk
* indicated by the contents of a cookie stored on the client's computer.
* This class is a static class with convenience methods for accessing the
* @tutorial PHPonTrax/Session.cls
* Name of the session (used as cookie name).
const TRAX_SESSION_NAME = "TRAXSESSID";
* Lifetime in seconds of cookie or, if 0, until browser is restarted.
const TRAX_SESSION_LIFETIME = "0";
* After this number of minutes, stored data will be seen as
* 'garbage' and cleaned up by the garbage collection process.
const TRAX_SESSION_MAXLIFETIME_MINUTES = "20";
private static $ip = null;
* User Agent (OS, Browser, etc) of client
private static $user_agent = null;
private static $started = false;
public static $id = null;
* Fetch the contents from a specified element of
* {@link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.session $_SESSION}
* @param mixed $key Key to identify one particular session variable
* of potentially many for this session
* @return mixed Content of the session variable with the specified
* key if the variable exists; otherwise null.
if(self::is_valid_host()) {
return $_SESSION[self::get_hash()][$key];
* Store a value in a specified element of
* {@link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.session $_SESSION}
* @param mixed $key Key to identify one particular session variable
* of potentially many for this session
* @param string $value Value to store in the session variable
function set($key, $value) {
if(self::is_valid_host()) {
$_SESSION[self::get_hash()][$key] = $value;
* Test whether the user host is as expected for this session
* Compare the REMOTE_ADDR and HTTP_USER_AGENT elements of
* {@link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server $_SERVER}
* to the expected values for this session.
* <li>true => User host is as expected</li>
* <li>false => User host NOT as expected</li>
if(($_SERVER['REMOTE_ADDR'] == self::$ip || self::is_aol_host()) &&
$_SERVER['HTTP_USER_AGENT'] == self::$user_agent) {
* Test whether the client is an AOL user
* Check whether the domain name of the client's IP ends in
* "proxy.aol.com" or the client's user agent name includes "AOL"
* <li>true => Client is on AOL</li>
* <li>false => Client from some other ISP</li>
stristr($_SERVER['HTTP_USER_AGENT'], "AOL")) {
* Get key that uniquely identifies this session
* Calculate a unique session key based on the session ID and
* user agent, plus the user's IP address if not on AOL.
if(!self::is_aol_host()) {
$key .= $_SERVER['REMOTE_ADDR'];
// error_log('get_hash() returns '.md5($key));
* Start or continue a session
$session_name = defined("TRAX_SESSION_NAME") ? TRAX_SESSION_NAME : self::TRAX_SESSION_NAME;
# set the session default for this app
ini_set('session.name', $session_name);
ini_set('session.cookie_lifetime', $session_lifetime);
ini_set('session.gc_probability', 1);
ini_set('session.gc_maxlifetime', $session_maxlifetime_minutes * 60);
header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
self::$ip = $_SERVER['REMOTE_ADDR'];
self::$user_agent = $_SERVER['HTTP_USER_AGENT'];
self::$id = session_id();
* Destroy the user's session
* Destroy all data registered to a session
* @uses session_destroy()
* Free all session variables currently registered
* Unset a session variable
* {@link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.session $_SESSION}
// error_log('Session::unset_var("'.$key.'")');
if(self::is_valid_host()) {
// error_log('before unsetting SESSION='.var_export($_SESSION,true));
unset ($_SESSION[self::get_hash()][$key]);
// error_log('after unsetting SESSION='.var_export($_SESSION,true));
* Test whether a session variable is defined in $_SESSION
* {@link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.session $_SESSION}
* array for the existance of a variable identified by $key
* @param mixed $key Key to identify one particular session variable
* of potentially many for this session
* <li>true => The specified session variable is
* <li>false => The specified session variable is
if(self::is_valid_host()) {
if($_SESSION[self::get_hash()][$key]) {
* Test whether there is a flash message to be shown
* {@link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.session $_SESSION}
* array for this session contains a
* flash message to be shown to the user.
* @param mixed $key Key to identify one particular flash message
* of potentially many for this session
* <li>true => A flash message is present</li>
* <li>false => No flash message is present</li>
if(self::is_valid_host()) {
$_SESSION[self::get_hash()]['flash'])) {
* Get or set a flash message
* A flash message is a message that will appear prominently on
* the next screen to be sent to the user. Flash
* messages are intended to be shown to the user once then erased.
* {@link http://www.php.net/manual/en/reserved.variables.php#reserved.variables.session $_SESSION}
* array for the user's session.
* @param mixed $key Key to identify one particular flash message
* of potentially many for this session
* @param string $value Content of the flash message if present
* @return mixed Content of the flash message with the specified
* key if $value is null; otherwise null.
function flash($key, $value = null) {
if(self::is_valid_host()) {
$_SESSION[self::get_hash()]['flash'][$key] = $value;
$value = $_SESSION[self::get_hash()]['flash'][$key];
unset ($_SESSION[self::get_hash()]['flash'][$key]);
// -- set Emacs parameters --
// c-hanging-comment-ender-p: nil
|