Execute('UPDATE '. ADODB_Session::table(). ' SET sesskey='. $conn->qstr($new_id). ' WHERE sesskey='.$conn->qstr($old_id)); /* it is possible that the update statement fails due to a collision */ if (!$ok) { session_id($old_id); if (empty($ck)) $ck = session_get_cookie_params(); setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']); return false; } return true; } /* Generate database table for session data @see http://phplens.com/lens/lensforum/msgs.php?id=12280 @return 0 if failure, 1 if errors, 2 if successful. @author Markus Staab http://www.public-4u.de */ function adodb_session_create_table($schemaFile=null,$conn = null) { // set default values if ($schemaFile===null) $schemaFile = ADODB_SESSION . '/session_schema.xml'; if ($conn===null) $conn =& ADODB_Session::_conn(); if (!$conn) return 0; $schema = new adoSchema($conn); $schema->ParseSchema($schemaFile); return $schema->ExecuteSchema(); } /*! \static */ class ADODB_Session { ///////////////////// // getter/setter methods ///////////////////// /* function Lock($lock=null) { static $_lock = false; if (!is_null($lock)) $_lock = $lock; return $lock; } */ /*! */ function driver($driver = null) { static $_driver = 'mysql'; static $set = false; if (!is_null($driver)) { $_driver = trim($driver); $set = true; } elseif (!$set) { // backwards compatibility if (isset($GLOBALS['ADODB_SESSION_DRIVER'])) { return $GLOBALS['ADODB_SESSION_DRIVER']; } } return $_driver; } /*! */ function host($host = null) { static $_host = 'localhost'; static $set = false; if (!is_null($host)) { $_host = trim($host); $set = true; } elseif (!$set) { // backwards compatibility if (isset($GLOBALS['ADODB_SESSION_CONNECT'])) { return $GLOBALS['ADODB_SESSION_CONNECT']; } } return $_host; } /*! */ function user($user = null) { static $_user = 'root'; static $set = false; if (!is_null($user)) { $_user = trim($user); $set = true; } elseif (!$set) { // backwards compatibility if (isset($GLOBALS['ADODB_SESSION_USER'])) { return $GLOBALS['ADODB_SESSION_USER']; } } return $_user; } /*! */ function password($password = null) { static $_password = ''; static $set = false; if (!is_null($password)) { $_password = $password; $set = true; } elseif (!$set) { // backwards compatibility if (isset($GLOBALS['ADODB_SESSION_PWD'])) { return $GLOBALS['ADODB_SESSION_PWD']; } } return $_password; } /*! */ function database($database = null) { static $_database = 'xphplens_2'; static $set = false; if (!is_null($database)) { $_database = trim($database); $set = true; } elseif (!$set) { // backwards compatibility if (isset($GLOBALS['ADODB_SESSION_DB'])) { return $GLOBALS['ADODB_SESSION_DB']; } } return $_database; } /*! */ function persist($persist = null) { static $_persist = true; if (!is_null($persist)) { $_persist = trim($persist); } return $_persist; } /*! */ function lifetime($lifetime = null) { static $_lifetime; static $set = false; if (!is_null($lifetime)) { $_lifetime = (int) $lifetime; $set = true; } elseif (!$set) { // backwards compatibility if (isset($GLOBALS['ADODB_SESS_LIFE'])) { return $GLOBALS['ADODB_SESS_LIFE']; } } if (!$_lifetime) { $_lifetime = ini_get('session.gc_maxlifetime'); if ($_lifetime <= 1) { // bug in PHP 4.0.3 pl 1 -- how about other versions? //print "
Session: connection failed
', false); return $ok; } /*! Close the connection */ function close() { /* $conn =& ADODB_Session::_conn(); if ($conn) $conn->Close(); */ return true; } /* Slurp in the session variables and return the serialized string */ function read($key) { $conn =& ADODB_Session::_conn(); $data = ADODB_Session::dataFieldName(); $filter = ADODB_Session::filter(); $table = ADODB_Session::table(); if (!$conn) { return ''; } assert('$table'); $qkey = $conn->quote($key); $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : ''; $sql = "SELECT $data FROM $table WHERE $binary sesskey = $qkey AND expiry >= " . time(); /* Lock code does not work as it needs to hold transaction within whole page, and we don't know if developer has commited elsewhere... :( */ #if (ADODB_Session::Lock()) # $rs =& $conn->RowLock($table, "$binary sesskey = $qkey AND expiry >= " . time(), $data); #else $rs =& $conn->Execute($sql); //ADODB_Session::_dumprs($rs); if ($rs) { if ($rs->EOF) { $v = ''; } else { $v = reset($rs->fields); $filter = array_reverse($filter); foreach ($filter as $f) { if (is_object($f)) { $v = $f->read($v, ADODB_Session::_sessionKey()); } } $v = rawurldecode($v); } $rs->Close(); ADODB_Session::_crc(strlen($v) . crc32($v)); return $v; } return ''; } /*! Write the serialized data to a database. If the data has not been modified since the last read(), we do not write. */ function write($key, $val) { $clob = ADODB_Session::clob(); $conn =& ADODB_Session::_conn(); $crc = ADODB_Session::_crc(); $data = ADODB_Session::dataFieldName(); $debug = ADODB_Session::debug(); $driver = ADODB_Session::driver(); $expire_notify = ADODB_Session::expireNotify(); $filter = ADODB_Session::filter(); $lifetime = ADODB_Session::lifetime(); $table = ADODB_Session::table(); if (!$conn) { return false; } $qkey = $conn->qstr($key); assert('$table'); $expiry = time() + $lifetime; $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : ''; // crc32 optimization since adodb 2.1 // now we only update expiry date, thx to sebastian thom in adodb 2.32 if ($crc !== false && $crc == (strlen($val) . crc32($val))) { if ($debug) { echo 'Session: Only updating date - crc32 not changed
'; } $sql = "UPDATE $table SET expiry = ".$conn->Param('0')." WHERE $binary sesskey = ".$conn->Param('1')." AND expiry >= ".$conn->Param('2'); $rs =& $conn->Execute($sql,array($expiry,$key,time())); ADODB_Session::_dumprs($rs); if ($rs) { $rs->Close(); } return true; } $val = rawurlencode($val); foreach ($filter as $f) { if (is_object($f)) { $val = $f->write($val, ADODB_Session::_sessionKey()); } } $arr = array('sesskey' => $key, 'expiry' => $expiry, $data => $val, 'expireref' => ''); if ($expire_notify) { $var = reset($expire_notify); global $$var; if (isset($$var)) { $arr['expireref'] = $$var; } } if (!$clob) { // no lobs, simply use replace() $arr[$data] = $conn->qstr($val); $rs = $conn->Replace($table, $arr, 'sesskey', $autoQuote = true); ADODB_Session::_dumprs($rs); } else { // what value shall we insert/update for lob row? switch ($driver) { // empty_clob or empty_lob for oracle dbs case 'oracle': case 'oci8': case 'oci8po': case 'oci805': $lob_value = sprintf('empty_%s()', strtolower($clob)); break; // null for all other default: $lob_value = 'null'; break; } // do we insert or update? => as for sesskey $rs =& $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = $qkey"); ADODB_Session::_dumprs($rs); if ($rs && reset($rs->fields) > 0) { $sql = "UPDATE $table SET expiry = $expiry, $data = $lob_value WHERE sesskey = $qkey"; } else { $sql = "INSERT INTO $table (expiry, $data, sesskey) VALUES ($expiry, $lob_value, $qkey)"; } if ($rs) { $rs->Close(); } $err = ''; $rs1 =& $conn->Execute($sql); ADODB_Session::_dumprs($rs1); if (!$rs1) { $err = $conn->ErrorMsg()."\n"; } $rs2 =& $conn->UpdateBlob($table, $data, $val, " sesskey=$qkey", strtoupper($clob)); ADODB_Session::_dumprs($rs2); if (!$rs2) { $err .= $conn->ErrorMsg()."\n"; } $rs = ($rs && $rs2) ? true : false; if ($rs1) { $rs1->Close(); } if (is_object($rs2)) { $rs2->Close(); } } if (!$rs) { ADOConnection::outp('Session Replace: ' . $conn->ErrorMsg() . '
', false); return false; } else { // bug in access driver (could be odbc?) means that info is not committed // properly unless select statement executed in Win2000 if ($conn->databaseType == 'access') { $sql = "SELECT sesskey FROM $table WHERE $binary sesskey = $qkey"; $rs =& $conn->Execute($sql); ADODB_Session::_dumprs($rs); if ($rs) { $rs->Close(); } } }/* if (ADODB_Session::Lock()) { $conn->CommitTrans(); }*/ return $rs ? true : false; } /*! */ function destroy($key) { $conn =& ADODB_Session::_conn(); $table = ADODB_Session::table(); $expire_notify = ADODB_Session::expireNotify(); if (!$conn) { return false; } assert('$table'); $qkey = $conn->quote($key); $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : ''; if ($expire_notify) { reset($expire_notify); $fn = next($expire_notify); $savem = $conn->SetFetchMode(ADODB_FETCH_NUM); $sql = "SELECT expireref, sesskey FROM $table WHERE $binary sesskey = $qkey"; $rs =& $conn->Execute($sql); ADODB_Session::_dumprs($rs); $conn->SetFetchMode($savem); if (!$rs) { return false; } if (!$rs->EOF) { $ref = $rs->fields[0]; $key = $rs->fields[1]; //assert('$ref'); //assert('$key'); $fn($ref, $key); } $rs->Close(); } $sql = "DELETE FROM $table WHERE $binary sesskey = $qkey"; $rs =& $conn->Execute($sql); ADODB_Session::_dumprs($rs); if ($rs) { $rs->Close(); } return $rs ? true : false; } /*! */ function gc($maxlifetime) { $conn =& ADODB_Session::_conn(); $debug = ADODB_Session::debug(); $expire_notify = ADODB_Session::expireNotify(); $optimize = ADODB_Session::optimize(); $sync_seconds = ADODB_Session::syncSeconds(); $table = ADODB_Session::table(); if (!$conn) { return false; } assert('$table'); $time = time(); $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : ''; if ($expire_notify) { reset($expire_notify); $fn = next($expire_notify); $savem = $conn->SetFetchMode(ADODB_FETCH_NUM); $sql = "SELECT expireref, sesskey FROM $table WHERE expiry < $time"; $rs =& $conn->Execute($sql); ADODB_Session::_dumprs($rs); $conn->SetFetchMode($savem); if ($rs) { $conn->BeginTrans(); $keys = array(); while (!$rs->EOF) { $ref = $rs->fields[0]; $key = $rs->fields[1]; $fn($ref, $key); $del = $conn->Execute("DELETE FROM $table WHERE sesskey='$key'"); $rs->MoveNext(); } $rs->Close(); $conn->CommitTrans(); } } else { $sql = "DELETE FROM $table WHERE expiry < $time"; $rs =& $conn->Execute($sql); ADODB_Session::_dumprs($rs); if ($rs) { $rs->Close(); } if ($debug) { ADOConnection::outp("Garbage Collection: $sql
"); } } // suggested by Cameron, "GaM3R"$msg
"); } } } } return true; } } ADODB_Session::_init(); // for backwards compatability only function adodb_sess_open($save_path, $session_name, $persist = true) { return ADODB_Session::open($save_path, $session_name, $persist); } // for backwards compatability only function adodb_sess_gc($t) { return ADODB_Session::gc($t); } ?>