Home » HTTP » HTTP_Session » Manual
The package provides an Object-oriented interface to the session_* family functions. It provides extra features such as database storage for session data using the DB, MDB and MDB2 package. It introduces new methods like isNew(), useCookies(), setExpire(), setIdle(), isExpired(), isIdled() and others.
Introduction
Introduction – Introduction to HTTP_Session
Overview
This package 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 more advanced features such as database containers, idle and expire timeouts, etc.
A few examples
Setting some options and detection of a new session
<?php
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
}
// after successful login use: HTTP_Session::regenerateId();
?>
Setting timeouts
<?php
HTTP_Session::start();
HTTP_Session::setExpire(time() + 60 * 60); // expires in one hour
HTTP_Session::setIdle(time() + 10 * 60); // idles in ten minutes
// expired
if (HTTP_Session::isExpired()) {
echo('Your session is expired!');
HTTP_Session::destroy();
}
// idled
if (HTTP_Session::isIdle()) {
echo('You've been idle for too long!');
HTTP_Session::destroy();
}
HTTP_Session::updateIdle();
?>
Database container
Database container – Store session data in a database with HTTP_Session
Overview
HTTP_Session lets you store the session data in a database making use of the DB, MDB and MDB2 packages.
Example
Using MDB2 to store session data in a database
<?php
/* create the following table in your database
CREATE TABLE sessiondata (
id varchar(32) NOT NULL,
expiry int(10),
data text,
PRIMARY KEY (id)
);
*/
require_once 'HTTP/Session.php';
HTTP_Session::useTransSID(false);
HTTP_Session::useCookies(false);
// enter your DSN
HTTP_Session::setContainer('MDB2', array('dsn' => 'mysql://root:password@localhost/database',
'table' => 'sessiondata'));
/*
// using an existing MDB2 connection
HTTP_Session::setContainer('MDB2', array('dsn' => &$db,
'table' => 'sessiondata'));
*/
HTTP_Session::start('s');
HTTP_Session::setExpire(time() + 60); // set expire to 60 seconds
HTTP_Session::setIdle(time() + 5); // set idle to 5 seconds
if (HTTP_Session::isExpired()) {
//HTTP_Session::replicate('sessiondata_backup'); // Replicate data of current session to specified table
HTTP_Session::destroy();
}
if (HTTP_Session::isIdle()) {
//HTTP_Session::replicate('sessiondata_backup'); // Replicate data of current session to specified table
HTTP_Session::destroy();
}
HTTP_Session::updateIdle();
?>