Source for file MDB.php
Documentation is available at MDB.php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002, Alexander Radivanovich |
// | 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: Lorenzo Alberton <l dot alberton at quipo dot it> |
// +-----------------------------------------------------------------------+
require_once 'HTTP/Session2/Container.php';
* Database container for session data
* Create the following table to store session data
* CREATE TABLE `sessiondata` (
* `id` CHAR(32) NOT NULL,
* `expiry` INT UNSIGNED NOT NULL DEFAULT 0,
* @author Lorenzo Alberton <l dot alberton at quipo dot it>
* $options is an array with the options.<br>
* <li>'dsn' - The DSN string</li>
* <li>'table' - Table with session data, default is 'sessiondata'</li>
* <li>'autooptimize' - Boolean, 'true' to optimize
* the table on garbage collection, default is 'false'.</li>
* @param array $options The options
$this->options['dsn'] = $options['dsn'];
$this->options['dsn'] = $options;
* Connect to database by using the given DSN string
* @param string DSN string
* @return mixed Object on error, otherwise bool
$this->db = MDB ::connect ($dsn);
} else if (is_object($dsn) && MDB ::isError ($dsn)) {
return new MDB_Error ($dsn->code , PEAR_ERROR_DIE );
return new PEAR_Error ("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__ ,
if (MDB ::isError ($this->db)) {
return new MDB_Error ($this->db->code , PEAR_ERROR_DIE );
* Set some default options
$this->options['dsn'] = null;
$this->options['table'] = 'sessiondata';
$this->options['autooptimize'] = false;
* Establish connection to a database
function open($save_path, $session_name)
if (MDB ::isError ($this->_connect ($this->options['dsn']))) {
$query = sprintf("SELECT data FROM %s WHERE id = %s AND expiry >= %d",
$this->db->getTextValue (md5($id)),
$result = $this->db->getOne ($query);
if (MDB ::isError ($result)) {
new MDB_Error ($result->code , PEAR_ERROR_DIE );
function write($id, $data)
if ((false !== $this->crc) && ($this->crc === strlen($data) . crc32($data))) {
// $_SESSION hasn't been touched, no need to update the blob column
$query = sprintf("UPDATE %s SET expiry = %d WHERE id = %s AND expiry >= %d",
$this->db->getTextValue (md5($id)),
// Check if table row already exists
$query = sprintf("SELECT COUNT(id) FROM %s WHERE id = %s",
$this->db->getTextValue (md5($id))
$result = $this->db->getOne ($query);
if (MDB ::isError ($result)) {
new MDB_Error ($result->code , PEAR_ERROR_DIE );
// Insert new row into table
$query = sprintf("INSERT INTO %s (id, expiry, data) VALUES (%s, %d, %s)",
$this->db->getTextValue (md5($id)),
$this->db->getTextValue ($data)
$query = sprintf("UPDATE %s SET expiry = %d, data = %s WHERE id = %s AND expiry >= %d",
$this->db->getTextValue ($data),
$this->db->getTextValue (md5($id)),
$result = $this->db->query ($query);
if (MDB ::isError ($result)) {
new MDB_Error ($result->code , PEAR_ERROR_DIE );
$query = sprintf("DELETE FROM %s WHERE id = %s",
$this->db->getTextValue (md5($id))
$result = $this->db->query ($query);
if (MDB ::isError ($result)) {
new MDB_Error ($result->code , PEAR_ERROR_DIE );
function gc($maxlifetime)
$query = sprintf("DELETE FROM %s WHERE expiry < %d",
$result = $this->db->query ($query);
if (MDB ::isError ($result)) {
new MDB_Error ($result->code , PEAR_ERROR_DIE );
if ($this->options['autooptimize']) {
switch($this->db->type ) {
$query = sprintf("OPTIMIZE TABLE %s", $this->options['table']);
$query = sprintf("VACUUM %s", $this->options['table']);
$result = $this->db->query ($query);
if (MDB ::isError ($result)) {
new MDB_Error ($result->code , PEAR_ERROR_DIE );
Documentation generated on Mon, 11 Mar 2019 14:18:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|