Session PDO Documentation

Requires PHP 5.2 or later

Description

“Session PDO” is a class based on PHP’s built-in session handler. It stores session data in a database using the PHP Data Objects (PDO) extension.

The use of the default PHP session handler is just fine in most situations. Session data is typically stored in files on same the server as where the web server runs. This may not always be desirable. Reasons to use a database instead can be:

Usage

Before you use “session PDO” be sure you have installed:

If these requirements are met, create the database/schema for the sessions. Then create the table session_data. SQL scripts are available for MySQL, Oracle, PostgreSQL and Sqlite. But you should be able to use any database that is supported by PDO.

Connect to the database by creating a PDO instance, e.g. $pdo = new PDO( 'mysql:host=localhost;dbname=sessions', 'user', 'password' ).

Start a session by creating a session instance, passing along the PDO instance, e.g. $session = new we_sessionPdo($pdo). You do not have to call session_start()

Optionally you can also use database transactions. Not all databases support this (like MySQL with the default MyISAM storage engine), e.g. $session = new we_sessionPdo($pdo, true).

The default session name is PDOSESSID. Optionally you can define your own, e.g. $session = new we_sessionPdo($pdo, false, 'CUSTOMSESSIONID').

After you created a session instance you can use built-in PHP session functionality. Data can be stored and retrieved using the $_SESSION variable. You cannot call functions that write session data, like session_write_close(), session_destroy() and session_regenerate_id(). Session data is written automatically by this class.

To destroy a session, call $session->destroy(). This not only destroys the session like session_destroy() does, but also empties the $_SESSION variable and deletes the session cookie.

To regenerate a session, call $session->regenerate_id().

Behaviour of sessions can be changed by editing the session runtime configuration.

Notes:

Examples

require_once 'sessionPdo.php';

// Start session using MySQL
try {
  $pdo = new PDO( 'mysql:host=localhost;dbname=sessions', 'user', 'password' );
  $session = new we_sessionPdo($pdo);
} catch( PDOException $e ) {
  die( $e->getMessage() );
}

// Start session using PostgreSQL using a persistent connection, transactions and custom session name
try {
  $pdo = new PDO( 'pgsql:host=localhost;dbname=sessions', 'user', 'password', array(PDO::ATTR_PERSISTENT => true) );
  $session = new we_sessionPdo($pdo, true,'MYSESSIONID');
} catch( PDOException $e ) {
  $pdo->rollBack();
  die( $e->getMessage() );
}

// Access session data as usual
$_SESSION['mydata'] = 'my session data'; // Add data to the session
echo $_SESSION['mydata']; // Get session data

// Regenerate session ID
$session->regenerate_id();

// Destroy session, automatically deleting session cookie and all session data
$session->destroy();

License

The package is released under the MIT License

Download

Reference

http://www.walterebert.com/code/