Using a database container

Using a database container – How to store session data in a database with HTTP_Session2

Overview

HTTP_Session lets you store the session data in a database making use of the DB 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/Session2.php';

HTTP_Session2::useTransSID(false);
HTTP_Session2::useCookies(false);

// enter your DSN
HTTP_Session2::setContainer('MDB2',
    array(
'dsn' => 'mysql://root:password@localhost/database',
        
'table' => 'sessiondata'));

/*
// using an existing MDB2 connection
HTTP_Session2::setContainer('MDB2',
    array('dsn'   => &$db, 'table' => 'sessiondata'));
*/

HTTP_Session2::start('s');
HTTP_Session2::setExpire(time() + 60); // set expire to 60 seconds
HTTP_Session2::setIdle(time() + 5);    // set idle to 5 seconds

if (HTTP_Session2::isExpired()) {
    
HTTP_Session2::destroy();
}

if (
HTTP_Session2::isIdle()) {
    
HTTP_Session2::destroy();
}

HTTP_Session2::updateIdle();
?>
An introduction to HTTP_Session2 (Previous) This example illustrates how to make a session persist over multiple browser sessions using a so called "session cookie". (Next)
Last updated: Tue, 19 Aug 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

Note by: 44gatti@gmail.com
setExpire(integer $time - Time in seconds)
in the example time() function instead of seconds