Source for file Session2.php
Documentation is available at Session2.php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2004, Tony Bibbs |
// | All rights reserved. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// +-----------------------------------------------------------------------+
// | Author: Tony Bibbs <tony@geeklog.net> |
// +-----------------------------------------------------------------------+
require_once 'PEAR/Exception.php';
/** @const HTTP_SESSION2_STARTED - The session was started with the current request */
define('HTTP_SESSION2_STARTED', 1 );
/** @const HTTP_SESSION2_STARTED - No new session was started with the current request */
define('HTTP_SESSION2_CONTINUED', 2 );
* Class for managing HTTP sessions
* Provides access to session-state values as well as session-level
* settings and lifetime management methods.
* Based on the standart PHP session handling mechanism
* it provides for you more advanced features such as
* database container, idle and expire timeouts, etc.
* // Setting some options and detecting of a new session
* HTTP_Session::setCookieless(false);
* HTTP_Session::start('MySessionID');
* HTTP_Session::set('variable', 'Tet string');
* if (HTTP_Session::isNew()) {
* echo('new session was created with the current request');
* $visitors++; // Increase visitors count
* // Using database container
* HTTP_Session::setContainer('DB');
* HTTP_Session::setExpire(time() + 60 * 60); // expires in one hour
* HTTP_Session::setIdle(10 * 60); // idles in ten minutes
* if (HTTP_Session::isExpired()) {
* echo('Your session is expired!');
* HTTP_Session::destroy();
* if (HTTP_Session::isIdle()) {
* echo('You've been idle for too long!');
* HTTP_Session::destroy();
* HTTP_Session::updateIdle();
* @author Alexander Radivaniovich <info@wwwlab.net>
* @author Tony Bibbs <tony@geeklog.net>
* Sets user-defined session storage functions
* Sets the user-defined session storage functions which are used
* for storing and retrieving data associated with a session.
* This is most useful when a storage method other than
* those supplied by PHP sessions is preferred.
* i.e. Storing the session data in a local database.
* @see session_set_save_handler()
public function setContainer($container, $container_options = null )
$container_class = 'HTTP_Session2_Container_' . $container;
$container_classfile = 'HTTP/Session2/Container/' . $container . '.php';
include_once $container_classfile;
throw new PEAR_Exception (" Container class, $container_class, does not exist" );
$container = new $container_class($container_options);
* Initializes session data
* Creates a session (or resumes the current one
* based on the session id being passed
* via a GET variable or a cookie).
* You can provide your own name and/or id for a session.
* @param $name string Name of a session, default is 'SessionID'
* @param $id string Id of a session which will be used
* only when the session is new
public function start($name = 'SessionID', $id = null )
if (!isset ($_SESSION['__HTTP_Session2_Info'])) {
* Writes session data and ends session
* Session data is usually stored after your script
* terminated without the need to call HTTP_Session::stop(),
* but as session data is locked to prevent concurrent
* writes only one script may operate on a session at any time.
* When using framesets together with sessions you will
* experience the frames loading one by one due to this
* locking. You can reduce the time needed to load all the
* frames by ending the session as soon as all changes
* to session variables are done.
* @see session_write_close()
public static function pause()
* Frees all session variables and destroys all data
* registered to a session
* This method resets the $_SESSION variable and
* destroys all of the data associated
* with the current session in its storage (file or DB).
* It forces new session to be started after this method
* is called. It does not unset the session cookie.
* Free all session variables
* @todo TODO Save expire and idle timestamps?
public static function clear()
$info = $_SESSION['__HTTP_Session2_Info'];
$_SESSION['__HTTP_Session2_Info'] = $info;
* Tries to find any session id in $_GET, $_POST or $_COOKIE
* @return string Session ID (if exists) or null
if (self ::useCookies ()) {
if (isset ($_COOKIE[self ::name ()])) {
return $_COOKIE[self ::name ()];
if (isset ($_GET[self ::name ()])) {
return $_GET[self ::name ()];
if (isset ($_POST[self ::name ()])) {
return $_POST[self ::name ()];
* Sets new name of a session
* @param string $name New name of a sesion
* @return string Previous name of a session
public static function name($name = null )
* Sets new ID of a session
* @param string $id New ID of a sesion
* @return string Previous ID of a session
public static function id($id = null )
* Sets the maximum expire time
* @param integer $time Time in seconds
* @param bool $add Add time to current expire time or not
public static function setExpire($time, $add = false )
$GLOBALS['__HTTP_Session2_Expire'] += $time;
$GLOBALS['__HTTP_Session2_Expire'] = $time;
if (!isset ($_SESSION['__HTTP_Session2_Expire_TS'])) {
$_SESSION['__HTTP_Session2_Expire_TS'] = time();
* Sets the maximum idle time
* Sets the time-out period allowed
* between requests before the session-state
* provider terminates the session.
* @param integer $time Time in seconds
* @param bool $add Add time to current maximum idle time or not
public static function setIdle($time, $add = false )
$GLOBALS['__HTTP_Session2_Idle'] += $time;
$GLOBALS['__HTTP_Session2_Idle'] = $time;
if (!isset ($_SESSION['__HTTP_Session2_Idle_TS'])) {
$_SESSION['__HTTP_Session2_Idle_TS'] = time();
* Returns the time up to the session is valid
* @return integer Time when the session idles
if (!isset ($_SESSION['__HTTP_Session2_Idle_TS']) || !isset ($GLOBALS['__HTTP_Session2_Idle'])) {
return $_SESSION['__HTTP_Session2_Idle_TS'] + $GLOBALS['__HTTP_Session2_Idle'];
* Check if session is expired
* @return boolean Obvious
if ($GLOBALS['__HTTP_Session2_Expire'] > 0 && isset ($_SESSION['__HTTP_Session2_Expire_TS']) &&
($_SESSION['__HTTP_Session2_Expire_TS'] + $GLOBALS['__HTTP_Session2_Expire']) <= time()) {
* Check if session is idle
* @return boolean Obvious
public static function isIdle()
if ($GLOBALS['__HTTP_Session2_Idle'] > 0 && isset ($_SESSION['__HTTP_Session2_Idle_TS']) &&
($_SESSION['__HTTP_Session2_Idle_TS'] + $GLOBALS['__HTTP_Session2_Idle']) <= time()) {
if (isset ($_SESSION['__HTTP_Session2_Idle_TS'])) {
$_SESSION['__HTTP_Session2_Idle_TS'] = time();
* If optional parameter is specified it indicates
* whether the module will use cookies to store
* the session id on the client side
* It returns the previous value of this property
* @param boolean $useCookies If specified it will replace the previous value
* @return boolean The previous value of the property
public static function useCookies($useCookies = null )
if (ini_get('session.use_cookies')) {
if (isset ($useCookies)) {
* Gets a value indicating whether the session
* was created with the current request
* You MUST call this method only after you have started
* the session with the HTTP_Session::start() method.
* @return boolean true if the session was created
* with the current request, false otherwise
public static function isNew()
// The best way to check if a session is new is to check
// for existence of a session data storage
// with the current session id, but this is impossible
// with the default PHP module wich is 'files'.
// So we need to emulate it.
return !isset ($_SESSION['__HTTP_Session2_Info']) ||
* Register variable with the current session
* @param string $name Name of a global variable
* @see session_register()
* Unregister a variable from the current session
* @param string $name Name of a global variable
* @see session_unregister()
session_unregister ($name);
* Returns session variable
* @param string $name Name of a variable
* @param mixed $default Default value of a variable if not set
* @return mixed Value of a variable
public static function &get($name, $default = null )
if (!isset ($_SESSION[$name]) && isset ($default)) {
$_SESSION[$name] = $default;
* @param string $name Name of a variable
* @param mixed $value Value of a variable
* @return mixed Old value of a variable
public function set($name, $value)
$return = @$_SESSION[$name];
$_SESSION[$name] = $value;
* Returns local variable of a script
* Two scripts can have local variables with the same names
* @param string $name Name of a variable
* @param mixed $default Default value of a variable if not set
* @return mixed Value of a local variable
public static function &getLocal($name, $default = null )
$local = md5(self ::localName ());
$_SESSION[$local] = array ();
if (!isset ($_SESSION[$local][$name]) && isset ($default)) {
$_SESSION[$local][$name] = $default;
return $_SESSION[$local][$name];
* Sets local variable of a script.
* Two scripts can have local variables with the same names.
* @param string $name Name of a local variable
* @param mixed $value Value of a local variable
* @return mixed Old value of a local variable
public static function setLocal($name, $value)
$local = md5(self ::localName ());
$_SESSION[$local] = array ();
$return = $_SESSION[$local][$name];
unset ($_SESSION[$local][$name]);
$_SESSION[$local][$name] = $value;
* @param string New local name
* @return string Previous local name
public static function localName($name = null )
$return = @$GLOBALS['__HTTP_Session2_Localname'];
$GLOBALS['__HTTP_Session2_Localname'] = $name;
public static function init()
// Disable auto-start of a sesion
// Set local name equal to the current script name
self ::localName ($_SERVER['SCRIPT_NAME']);
Documentation generated on Mon, 11 Mar 2019 14:18:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|