adodb 최신버전으로 변경

<? 를 <?php 로 변경
This commit is contained in:
2018-01-11 19:28:35 +09:00
parent feff33715f
commit 7b3b674dee
450 changed files with 19425 additions and 32248 deletions
+47 -46
View File
@@ -1,26 +1,28 @@
<?php
/*
V5.09 25 June 2009 (c) 2000-2009 John Lim (jlim#natsoft.com). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
@version v5.20.9 21-Dec-2016
@copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
@copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
Made table name configurable - by David Johnson djohnson@inpro.net
Encryption by Ari Kuorikoski <ari.kuorikoski@finebyte.com>
Set tabs to 4 for best viewing.
Latest version of ADODB is available at http://php.weblogs.com/adodb
======================================================================
This file provides PHP4 session management using the ADODB database
wrapper library.
Example
=======
include('adodb.inc.php');
#---------------------------------#
include('adodb-cryptsession.php');
include('adodb-cryptsession.php');
#---------------------------------#
session_start();
session_register('AVAR');
@@ -28,12 +30,12 @@ wrapper library.
print "
-- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
Installation
============
1. Create a new database in MySQL or Access "sessions" like
so:
create table sessions (
SESSKEY char(32) not null,
EXPIRY int(11) unsigned not null,
@@ -41,17 +43,17 @@ so:
DATA CLOB,
primary key (sesskey)
);
2. Then define the following parameters. You can either modify
this file, or define them before this file is included:
$ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase';
$ADODB_SESSION_CONNECT='server to connect to';
$ADODB_SESSION_USER ='user';
$ADODB_SESSION_PWD ='password';
$ADODB_SESSION_DB ='database';
$ADODB_SESSION_TBL = 'sessions'
3. Recommended is PHP 4.0.2 or later. There are documented
session bugs in earlier versions of PHP.
@@ -65,13 +67,13 @@ if (!defined('_ADODB_LAYER')) {
}
/* if database time and system time is difference is greater than this, then give warning */
define('ADODB_SESSION_SYNCH_SECS',60);
define('ADODB_SESSION_SYNCH_SECS',60);
if (!defined('ADODB_SESSION')) {
define('ADODB_SESSION',1);
GLOBAL $ADODB_SESSION_CONNECT,
GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_DRIVER,
$ADODB_SESSION_USER,
$ADODB_SESSION_PWD,
@@ -81,10 +83,10 @@ GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESS_DEBUG,
$ADODB_SESS_INSERT,
$ADODB_SESSION_EXPIRE_NOTIFY,
$ADODB_SESSION_TBL;
$ADODB_SESSION_TBL;
//$ADODB_SESS_DEBUG = true;
/* SET THE FOLLOWING PARAMETERS */
if (empty($ADODB_SESSION_DRIVER)) {
$ADODB_SESSION_DRIVER='mysql';
@@ -102,7 +104,7 @@ if (empty($ADODB_SESSION_EXPIRE_NOTIFY)) {
$ADODB_SESSION_EXPIRE_NOTIFY = false;
}
function ADODB_Session_Key()
function ADODB_Session_Key()
{
$ADODB_CRYPT_KEY = 'CRYPTED ADODB SESSIONS ROCK!';
@@ -118,20 +120,20 @@ if ($ADODB_SESS_LIFE <= 1) {
$ADODB_SESS_LIFE=1440;
}
function adodb_sess_open($save_path, $session_name)
function adodb_sess_open($save_path, $session_name)
{
GLOBAL $ADODB_SESSION_CONNECT,
GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_DRIVER,
$ADODB_SESSION_USER,
$ADODB_SESSION_PWD,
$ADODB_SESSION_DB,
$ADODB_SESS_CONN,
$ADODB_SESS_DEBUG;
$ADODB_SESS_INSERT = false;
if (isset($ADODB_SESS_CONN)) return true;
$ADODB_SESS_CONN = ADONewConnection($ADODB_SESSION_DRIVER);
if (!empty($ADODB_SESS_DEBUG)) {
$ADODB_SESS_CONN->debug = true;
@@ -139,10 +141,10 @@ GLOBAL $ADODB_SESSION_CONNECT,
}
return $ADODB_SESS_CONN->PConnect($ADODB_SESSION_CONNECT,
$ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
}
function adodb_sess_close()
function adodb_sess_close()
{
global $ADODB_SESS_CONN;
@@ -150,7 +152,7 @@ global $ADODB_SESS_CONN;
return true;
}
function adodb_sess_read($key)
function adodb_sess_read($key)
{
$Crypt = new MD5Crypt;
global $ADODB_SESS_CONN,$ADODB_SESS_INSERT,$ADODB_SESSION_TBL;
@@ -167,20 +169,20 @@ global $ADODB_SESS_CONN,$ADODB_SESS_INSERT,$ADODB_SESSION_TBL;
return $v;
}
else $ADODB_SESS_INSERT = true;
return '';
}
function adodb_sess_write($key, $val)
function adodb_sess_write($key, $val)
{
$Crypt = new MD5Crypt;
global $ADODB_SESS_INSERT,$ADODB_SESS_CONN, $ADODB_SESS_LIFE, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
$expiry = time() + $ADODB_SESS_LIFE;
// encrypt session data..
// encrypt session data..
$val = $Crypt->Encrypt(rawurlencode($val), ADODB_Session_Key());
$arr = array('sesskey' => $key, 'expiry' => $expiry, 'data' => $val);
if ($ADODB_SESSION_EXPIRE_NOTIFY) {
$var = reset($ADODB_SESSION_EXPIRE_NOTIFY);
@@ -197,16 +199,16 @@ $Crypt = new MD5Crypt;
} else {
// bug in access driver (could be odbc?) means that info is not commited
// properly unless select statement executed in Win2000
if ($ADODB_SESS_CONN->databaseType == 'access') $rs = $ADODB_SESS_CONN->Execute("select sesskey from $ADODB_SESSION_TBL WHERE sesskey='$key'");
}
return isset($rs);
}
function adodb_sess_destroy($key)
function adodb_sess_destroy($key)
{
global $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
if ($ADODB_SESSION_EXPIRE_NOTIFY) {
reset($ADODB_SESSION_EXPIRE_NOTIFY);
$fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
@@ -252,7 +254,7 @@ function adodb_sess_gc($maxlifetime) {
$rs->MoveNext();
}
$rs->Close();
$ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE expiry < $t");
$ADODB_SESS_CONN->CommitTrans();
}
@@ -260,12 +262,12 @@ function adodb_sess_gc($maxlifetime) {
$qry = "DELETE FROM $ADODB_SESSION_TBL WHERE expiry < " . time();
$ADODB_SESS_CONN->Execute($qry);
}
// suggested by Cameron, "GaM3R" <gamr@outworld.cx>
if (defined('ADODB_SESSION_OPTIMIZE'))
{
global $ADODB_SESSION_DRIVER;
switch( $ADODB_SESSION_DRIVER ) {
case 'mysql':
case 'mysqlt':
@@ -273,34 +275,34 @@ function adodb_sess_gc($maxlifetime) {
break;
case 'postgresql':
case 'postgresql7':
$opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;
$opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;
break;
}
}
if ($ADODB_SESS_CONN->dataProvider === 'oci8') $sql = 'select TO_CHAR('.($ADODB_SESS_CONN->sysTimeStamp).', \'RRRR-MM-DD HH24:MI:SS\') from '. $ADODB_SESSION_TBL;
else $sql = 'select '.$ADODB_SESS_CONN->sysTimeStamp.' from '. $ADODB_SESSION_TBL;
$rs = $ADODB_SESS_CONN->SelectLimit($sql,1);
if ($rs && !$rs->EOF) {
$dbts = reset($rs->fields);
$rs->Close();
$dbt = $ADODB_SESS_CONN->UnixTimeStamp($dbts);
$t = time();
if (abs($dbt - $t) >= ADODB_SESSION_SYNCH_SECS) {
$msg =
$msg =
__FILE__.": Server time for webserver {$_SERVER['HTTP_HOST']} not in synch with database: database=$dbt ($dbts), webserver=$t (diff=".(abs($dbt-$t)/3600)." hrs)";
error_log($msg);
if ($ADODB_SESS_DEBUG) ADOConnection::outp("
-- $msg</p>");
}
}
return true;
}
session_module_name('user');
session_module_name('user');
session_set_save_handler(
"adodb_sess_open",
"adodb_sess_close",
@@ -321,4 +323,3 @@ if (0) {
-- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
}
*/
?>
+70 -70
View File
@@ -1,20 +1,22 @@
<?php
/*
V4.93 10 Oct 2006 (c) 2000-2009 John Lim (jlim#natsoft.com). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
@version v5.20.9 21-Dec-2016
@copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
@copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
Set tabs to 4 for best viewing.
Latest version of ADODB is available at http://php.weblogs.com/adodb
======================================================================
This file provides PHP4 session management using the ADODB database
wrapper library, using Oracle CLOB's to store data. Contributed by achim.gosse@ddd.de.
Example
=======
include('adodb.inc.php');
include('adodb-session.php');
session_start();
@@ -22,7 +24,7 @@
$_SESSION['AVAR'] += 1;
print "
-- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
To force non-persistent connections, call adodb_session_open first before session_start():
include('adodb.inc.php');
@@ -34,11 +36,11 @@ To force non-persistent connections, call adodb_session_open first before sessio
print "
-- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
Installation
============
1. Create this table in your database (syntax might vary depending on your db):
create table sessions (
SESSKEY char(32) not null,
EXPIRY int(11) unsigned not null,
@@ -56,7 +58,7 @@ To force non-persistent connections, call adodb_session_open first before sessio
$ADODB_SESSION_DB ='database';
$ADODB_SESSION_TBL = 'sessions'
$ADODB_SESSION_USE_LOBS = false; (or, if you wanna use CLOBS (= 'CLOB') or ( = 'BLOB')
3. Recommended is PHP 4.1.0 or later. There are documented
session bugs in earlier versions of PHP.
@@ -64,24 +66,24 @@ To force non-persistent connections, call adodb_session_open first before sessio
you can tag a session with an EXPIREREF, and before the session
record is deleted, we can call a function that will pass the EXPIREREF
as the first parameter, and the session key as the second parameter.
To do this, define a notification function, say NotifyFn:
function NotifyFn($expireref, $sesskey)
{
}
Then you need to define a global variable $ADODB_SESSION_EXPIRE_NOTIFY.
This is an array with 2 elements, the first being the name of the variable
you would like to store in the EXPIREREF field, and the 2nd is the
you would like to store in the EXPIREREF field, and the 2nd is the
notification function's name.
In this example, we want to be notified when a user's session
has expired, so we store the user id in the global variable $USERID,
In this example, we want to be notified when a user's session
has expired, so we store the user id in the global variable $USERID,
store this value in the EXPIREREF field:
$ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');
Then when the NotifyFn is called, we are passed the $USERID as the first
parameter, eg. NotifyFn($userid, $sesskey).
*/
@@ -93,14 +95,14 @@ if (!defined('_ADODB_LAYER')) {
if (!defined('ADODB_SESSION')) {
define('ADODB_SESSION',1);
/* if database time and system time is difference is greater than this, then give warning */
define('ADODB_SESSION_SYNCH_SECS',60);
define('ADODB_SESSION_SYNCH_SECS',60);
/****************************************************************************************\
Global definitions
\****************************************************************************************/
GLOBAL $ADODB_SESSION_CONNECT,
GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_DRIVER,
$ADODB_SESSION_USER,
$ADODB_SESSION_PWD,
@@ -112,9 +114,9 @@ GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_CRC,
$ADODB_SESSION_USE_LOBS,
$ADODB_SESSION_TBL;
if (!isset($ADODB_SESSION_USE_LOBS)) $ADODB_SESSION_USE_LOBS = 'CLOB';
$ADODB_SESS_LIFE = ini_get('session.gc_maxlifetime');
if ($ADODB_SESS_LIFE <= 1) {
// bug in PHP 4.0.3 pl 1 -- how about other versions?
@@ -123,11 +125,11 @@ GLOBAL $ADODB_SESSION_CONNECT,
}
$ADODB_SESSION_CRC = false;
//$ADODB_SESS_DEBUG = true;
//////////////////////////////////
/* SET THE FOLLOWING PARAMETERS */
//////////////////////////////////
if (empty($ADODB_SESSION_DRIVER)) {
$ADODB_SESSION_DRIVER='mysql';
$ADODB_SESSION_CONNECT='localhost';
@@ -135,7 +137,7 @@ GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_PWD ='';
$ADODB_SESSION_DB ='xphplens_2';
}
if (empty($ADODB_SESSION_EXPIRE_NOTIFY)) {
$ADODB_SESSION_EXPIRE_NOTIFY = false;
}
@@ -143,7 +145,7 @@ GLOBAL $ADODB_SESSION_CONNECT,
if (empty($ADODB_SESSION_TBL)){
$ADODB_SESSION_TBL = 'sessions';
}
// defaulting $ADODB_SESSION_USE_LOBS
if (!isset($ADODB_SESSION_USE_LOBS) || empty($ADODB_SESSION_USE_LOBS)) {
@@ -158,28 +160,28 @@ GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESS['db'] = $ADODB_SESSION_DB;
$ADODB_SESS['life'] = $ADODB_SESS_LIFE;
$ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
$ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
$ADODB_SESS['table'] = $ADODB_SESS_TBL;
*/
/****************************************************************************************\
Create the connection to the database.
Create the connection to the database.
If $ADODB_SESS_CONN already exists, reuse that connection
\****************************************************************************************/
function adodb_sess_open($save_path, $session_name,$persist=true)
function adodb_sess_open($save_path, $session_name,$persist=true)
{
GLOBAL $ADODB_SESS_CONN;
if (isset($ADODB_SESS_CONN)) return true;
GLOBAL $ADODB_SESSION_CONNECT,
GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_DRIVER,
$ADODB_SESSION_USER,
$ADODB_SESSION_PWD,
$ADODB_SESSION_DB,
$ADODB_SESS_DEBUG;
// cannot use & below - do not know why...
$ADODB_SESS_CONN = ADONewConnection($ADODB_SESSION_DRIVER);
if (!empty($ADODB_SESS_DEBUG)) {
@@ -190,7 +192,7 @@ GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
else $ok = $ADODB_SESS_CONN->Connect($ADODB_SESSION_CONNECT,
$ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
if (!$ok) ADOConnection::outp( "
-- Session: connection failed</p>",false);
}
@@ -198,7 +200,7 @@ GLOBAL $ADODB_SESSION_CONNECT,
/****************************************************************************************\
Close the connection
\****************************************************************************************/
function adodb_sess_close()
function adodb_sess_close()
{
global $ADODB_SESS_CONN;
@@ -209,7 +211,7 @@ global $ADODB_SESS_CONN;
/****************************************************************************************\
Slurp in the session variables and return the serialized string
\****************************************************************************************/
function adodb_sess_read($key)
function adodb_sess_read($key)
{
global $ADODB_SESS_CONN,$ADODB_SESSION_TBL,$ADODB_SESSION_CRC;
@@ -217,50 +219,50 @@ global $ADODB_SESS_CONN,$ADODB_SESSION_TBL,$ADODB_SESSION_CRC;
if ($rs) {
if ($rs->EOF) {
$v = '';
} else
} else
$v = rawurldecode(reset($rs->fields));
$rs->Close();
// new optimization adodb 2.1
$ADODB_SESSION_CRC = strlen($v).crc32($v);
return $v;
}
return ''; // thx to Jorma Tuomainen, webmaster#wizactive.com
}
/****************************************************************************************\
Write the serialized data to a database.
If the data has not been modified since adodb_sess_read(), we do not write.
\****************************************************************************************/
function adodb_sess_write($key, $val)
function adodb_sess_write($key, $val)
{
global
$ADODB_SESS_CONN,
$ADODB_SESS_LIFE,
$ADODB_SESS_CONN,
$ADODB_SESS_LIFE,
$ADODB_SESSION_TBL,
$ADODB_SESS_DEBUG,
$ADODB_SESS_DEBUG,
$ADODB_SESSION_CRC,
$ADODB_SESSION_EXPIRE_NOTIFY,
$ADODB_SESSION_DRIVER, // added
$ADODB_SESSION_USE_LOBS; // added
$expiry = time() + $ADODB_SESS_LIFE;
// crc32 optimization since adodb 2.1
// now we only update expiry date, thx to sebastian thom in adodb 2.32
if ($ADODB_SESSION_CRC !== false && $ADODB_SESSION_CRC == strlen($val).crc32($val)) {
if ($ADODB_SESS_DEBUG) echo "
-- Session: Only updating date - crc32 not changed</p>";
$qry = "UPDATE $ADODB_SESSION_TBL SET expiry=$expiry WHERE sesskey='$key' AND expiry >= " . time();
$rs = $ADODB_SESS_CONN->Execute($qry);
$rs = $ADODB_SESS_CONN->Execute($qry);
return true;
}
$val = rawurlencode($val);
$arr = array('sesskey' => $key, 'expiry' => $expiry, 'data' => $val);
if ($ADODB_SESSION_EXPIRE_NOTIFY) {
$var = reset($ADODB_SESSION_EXPIRE_NOTIFY);
@@ -268,7 +270,7 @@ function adodb_sess_write($key, $val)
$arr['expireref'] = $$var;
}
if ($ADODB_SESSION_USE_LOBS === false) { // no lobs, simply use replace()
$rs = $ADODB_SESS_CONN->Replace($ADODB_SESSION_TBL,$arr, 'sesskey',$autoQuote = true);
if (!$rs) {
@@ -318,16 +320,16 @@ function adodb_sess_write($key, $val)
} else {
// bug in access driver (could be odbc?) means that info is not commited
// properly unless select statement executed in Win2000
if ($ADODB_SESS_CONN->databaseType == 'access')
if ($ADODB_SESS_CONN->databaseType == 'access')
$rs = $ADODB_SESS_CONN->Execute("select sesskey from $ADODB_SESSION_TBL WHERE sesskey='$key'");
}
return !empty($rs);
}
function adodb_sess_destroy($key)
function adodb_sess_destroy($key)
{
global $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
if ($ADODB_SESSION_EXPIRE_NOTIFY) {
reset($ADODB_SESSION_EXPIRE_NOTIFY);
$fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
@@ -352,10 +354,10 @@ function adodb_sess_destroy($key)
return $rs ? true : false;
}
function adodb_sess_gc($maxlifetime)
function adodb_sess_gc($maxlifetime)
{
global $ADODB_SESS_DEBUG, $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
if ($ADODB_SESSION_EXPIRE_NOTIFY) {
reset($ADODB_SESSION_EXPIRE_NOTIFY);
$fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
@@ -373,21 +375,21 @@ function adodb_sess_gc($maxlifetime)
$rs->MoveNext();
}
$rs->Close();
//$ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE expiry < $t");
$ADODB_SESS_CONN->CommitTrans();
}
} else {
$ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE expiry < " . time());
if ($ADODB_SESS_DEBUG) ADOConnection::outp("
-- <b>Garbage Collection</b>: $qry</p>");
}
// suggested by Cameron, "GaM3R" <gamr@outworld.cx>
if (defined('ADODB_SESSION_OPTIMIZE')) {
global $ADODB_SESSION_DRIVER;
switch( $ADODB_SESSION_DRIVER ) {
case 'mysql':
case 'mysqlt':
@@ -395,7 +397,7 @@ function adodb_sess_gc($maxlifetime)
break;
case 'postgresql':
case 'postgresql7':
$opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;
$opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;
break;
}
if (!empty($opt_qry)) {
@@ -404,27 +406,27 @@ function adodb_sess_gc($maxlifetime)
}
if ($ADODB_SESS_CONN->dataProvider === 'oci8') $sql = 'select TO_CHAR('.($ADODB_SESS_CONN->sysTimeStamp).', \'RRRR-MM-DD HH24:MI:SS\') from '. $ADODB_SESSION_TBL;
else $sql = 'select '.$ADODB_SESS_CONN->sysTimeStamp.' from '. $ADODB_SESSION_TBL;
$rs = $ADODB_SESS_CONN->SelectLimit($sql,1);
if ($rs && !$rs->EOF) {
$dbts = reset($rs->fields);
$rs->Close();
$dbt = $ADODB_SESS_CONN->UnixTimeStamp($dbts);
$t = time();
if (abs($dbt - $t) >= ADODB_SESSION_SYNCH_SECS) {
$msg =
$msg =
__FILE__.": Server time for webserver {$_SERVER['HTTP_HOST']} not in synch with database: database=$dbt ($dbts), webserver=$t (diff=".(abs($dbt-$t)/3600)." hrs)";
error_log($msg);
if ($ADODB_SESS_DEBUG) ADOConnection::outp("
-- $msg</p>");
}
}
return true;
}
session_module_name('user');
session_module_name('user');
session_set_save_handler(
"adodb_sess_open",
"adodb_sess_close",
@@ -444,5 +446,3 @@ if (0) {
ADOConnection::outp( "
-- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>",false);
}
?>
+78 -78
View File
@@ -1,20 +1,22 @@
<?php
/*
V4.93 10 Oct 2006 (c) 2000-2009 John Lim (jlim#natsoft.com). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
@version v5.20.9 21-Dec-2016
@copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
@copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
Set tabs to 4 for best viewing.
Latest version of ADODB is available at http://php.weblogs.com/adodb
======================================================================
This file provides PHP4 session management using the ADODB database
wrapper library.
Example
=======
include('adodb.inc.php');
include('adodb-session.php');
session_start();
@@ -22,7 +24,7 @@ wrapper library.
$_SESSION['AVAR'] += 1;
print "
-- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
To force non-persistent connections, call adodb_session_open first before session_start():
include('adodb.inc.php');
@@ -34,11 +36,11 @@ To force non-persistent connections, call adodb_session_open first before sessio
print "
-- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
Installation
============
1. Create this table in your database (syntax might vary depending on your db):
create table sessions (
SESSKEY char(32) not null,
EXPIRY int(11) unsigned not null,
@@ -46,7 +48,7 @@ To force non-persistent connections, call adodb_session_open first before sessio
DATA text not null,
primary key (sesskey)
);
For oracle:
create table sessions (
SESSKEY char(32) not null,
@@ -59,14 +61,14 @@ To force non-persistent connections, call adodb_session_open first before sessio
2. Then define the following parameters. You can either modify
this file, or define them before this file is included:
$ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase';
$ADODB_SESSION_CONNECT='server to connect to';
$ADODB_SESSION_USER ='user';
$ADODB_SESSION_PWD ='password';
$ADODB_SESSION_DB ='database';
$ADODB_SESSION_TBL = 'sessions'
3. Recommended is PHP 4.1.0 or later. There are documented
session bugs in earlier versions of PHP.
@@ -74,24 +76,24 @@ To force non-persistent connections, call adodb_session_open first before sessio
you can tag a session with an EXPIREREF, and before the session
record is deleted, we can call a function that will pass the EXPIREREF
as the first parameter, and the session key as the second parameter.
To do this, define a notification function, say NotifyFn:
function NotifyFn($expireref, $sesskey)
{
}
Then you need to define a global variable $ADODB_SESSION_EXPIRE_NOTIFY.
This is an array with 2 elements, the first being the name of the variable
you would like to store in the EXPIREREF field, and the 2nd is the
you would like to store in the EXPIREREF field, and the 2nd is the
notification function's name.
In this example, we want to be notified when a user's session
has expired, so we store the user id in the global variable $USERID,
In this example, we want to be notified when a user's session
has expired, so we store the user id in the global variable $USERID,
store this value in the EXPIREREF field:
$ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');
Then when the NotifyFn is called, we are passed the $USERID as the first
parameter, eg. NotifyFn($userid, $sesskey).
*/
@@ -103,14 +105,14 @@ if (!defined('_ADODB_LAYER')) {
if (!defined('ADODB_SESSION')) {
define('ADODB_SESSION',1);
/* if database time and system time is difference is greater than this, then give warning */
define('ADODB_SESSION_SYNCH_SECS',60);
define('ADODB_SESSION_SYNCH_SECS',60);
/*
Thanks Joe Li. See http://phplens.com/lens/lensforum/msgs.php?id=11487&x=1
*/
function adodb_session_regenerate_id()
function adodb_session_regenerate_id()
{
$conn = ADODB_Session::_conn();
if (!$conn) return false;
@@ -126,7 +128,7 @@ function adodb_session_regenerate_id()
}
$new_id = session_id();
$ok = $conn->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);
@@ -134,14 +136,14 @@ function adodb_session_regenerate_id()
setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']);
return false;
}
return true;
}
/****************************************************************************************\
Global definitions
\****************************************************************************************/
GLOBAL $ADODB_SESSION_CONNECT,
GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_DRIVER,
$ADODB_SESSION_USER,
$ADODB_SESSION_PWD,
@@ -152,8 +154,8 @@ GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_EXPIRE_NOTIFY,
$ADODB_SESSION_CRC,
$ADODB_SESSION_TBL;
$ADODB_SESS_LIFE = ini_get('session.gc_maxlifetime');
if ($ADODB_SESS_LIFE <= 1) {
// bug in PHP 4.0.3 pl 1 -- how about other versions?
@@ -162,11 +164,11 @@ GLOBAL $ADODB_SESSION_CONNECT,
}
$ADODB_SESSION_CRC = false;
//$ADODB_SESS_DEBUG = true;
//////////////////////////////////
/* SET THE FOLLOWING PARAMETERS */
//////////////////////////////////
if (empty($ADODB_SESSION_DRIVER)) {
$ADODB_SESSION_DRIVER='mysql';
$ADODB_SESSION_CONNECT='localhost';
@@ -174,7 +176,7 @@ GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_PWD ='';
$ADODB_SESSION_DB ='xphplens_2';
}
if (empty($ADODB_SESSION_EXPIRE_NOTIFY)) {
$ADODB_SESSION_EXPIRE_NOTIFY = false;
}
@@ -182,7 +184,7 @@ GLOBAL $ADODB_SESSION_CONNECT,
if (empty($ADODB_SESSION_TBL)){
$ADODB_SESSION_TBL = 'sessions';
}
/*
$ADODB_SESS['driver'] = $ADODB_SESSION_DRIVER;
$ADODB_SESS['connect'] = $ADODB_SESSION_CONNECT;
@@ -191,28 +193,28 @@ GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESS['db'] = $ADODB_SESSION_DB;
$ADODB_SESS['life'] = $ADODB_SESS_LIFE;
$ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
$ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
$ADODB_SESS['table'] = $ADODB_SESS_TBL;
*/
/****************************************************************************************\
Create the connection to the database.
Create the connection to the database.
If $ADODB_SESS_CONN already exists, reuse that connection
\****************************************************************************************/
function adodb_sess_open($save_path, $session_name,$persist=true)
function adodb_sess_open($save_path, $session_name,$persist=true)
{
GLOBAL $ADODB_SESS_CONN;
if (isset($ADODB_SESS_CONN)) return true;
GLOBAL $ADODB_SESSION_CONNECT,
GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_DRIVER,
$ADODB_SESSION_USER,
$ADODB_SESSION_PWD,
$ADODB_SESSION_DB,
$ADODB_SESS_DEBUG;
// cannot use & below - do not know why...
$ADODB_SESS_CONN = ADONewConnection($ADODB_SESSION_DRIVER);
if (!empty($ADODB_SESS_DEBUG)) {
@@ -223,7 +225,7 @@ GLOBAL $ADODB_SESSION_CONNECT,
$ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
else $ok = $ADODB_SESS_CONN->Connect($ADODB_SESSION_CONNECT,
$ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
if (!$ok) ADOConnection::outp( "
-- Session: connection failed</p>",false);
}
@@ -231,7 +233,7 @@ GLOBAL $ADODB_SESSION_CONNECT,
/****************************************************************************************\
Close the connection
\****************************************************************************************/
function adodb_sess_close()
function adodb_sess_close()
{
global $ADODB_SESS_CONN;
@@ -242,7 +244,7 @@ global $ADODB_SESS_CONN;
/****************************************************************************************\
Slurp in the session variables and return the serialized string
\****************************************************************************************/
function adodb_sess_read($key)
function adodb_sess_read($key)
{
global $ADODB_SESS_CONN,$ADODB_SESSION_TBL,$ADODB_SESSION_CRC;
@@ -250,48 +252,48 @@ global $ADODB_SESS_CONN,$ADODB_SESSION_TBL,$ADODB_SESSION_CRC;
if ($rs) {
if ($rs->EOF) {
$v = '';
} else
} else
$v = rawurldecode(reset($rs->fields));
$rs->Close();
// new optimization adodb 2.1
$ADODB_SESSION_CRC = strlen($v).crc32($v);
return $v;
}
return ''; // thx to Jorma Tuomainen, webmaster#wizactive.com
}
/****************************************************************************************\
Write the serialized data to a database.
If the data has not been modified since adodb_sess_read(), we do not write.
\****************************************************************************************/
function adodb_sess_write($key, $val)
function adodb_sess_write($key, $val)
{
global
$ADODB_SESS_CONN,
$ADODB_SESS_LIFE,
$ADODB_SESS_CONN,
$ADODB_SESS_LIFE,
$ADODB_SESSION_TBL,
$ADODB_SESS_DEBUG,
$ADODB_SESS_DEBUG,
$ADODB_SESSION_CRC,
$ADODB_SESSION_EXPIRE_NOTIFY;
$expiry = time() + $ADODB_SESS_LIFE;
// crc32 optimization since adodb 2.1
// now we only update expiry date, thx to sebastian thom in adodb 2.32
if ($ADODB_SESSION_CRC !== false && $ADODB_SESSION_CRC == strlen($val).crc32($val)) {
if ($ADODB_SESS_DEBUG) echo "
-- Session: Only updating date - crc32 not changed</p>";
$qry = "UPDATE $ADODB_SESSION_TBL SET expiry=$expiry WHERE sesskey='$key' AND expiry >= " . time();
$rs = $ADODB_SESS_CONN->Execute($qry);
$rs = $ADODB_SESS_CONN->Execute($qry);
return true;
}
$val = rawurlencode($val);
$arr = array('sesskey' => $key, 'expiry' => $expiry, 'data' => $val);
if ($ADODB_SESSION_EXPIRE_NOTIFY) {
$var = reset($ADODB_SESSION_EXPIRE_NOTIFY);
@@ -300,23 +302,23 @@ function adodb_sess_write($key, $val)
}
$rs = $ADODB_SESS_CONN->Replace($ADODB_SESSION_TBL,$arr,
'sesskey',$autoQuote = true);
if (!$rs) {
ADOConnection::outp( '
-- Session Replace: '.$ADODB_SESS_CONN->ErrorMsg().'</p>',false);
} else {
// bug in access driver (could be odbc?) means that info is not commited
// properly unless select statement executed in Win2000
if ($ADODB_SESS_CONN->databaseType == 'access')
if ($ADODB_SESS_CONN->databaseType == 'access')
$rs = $ADODB_SESS_CONN->Execute("select sesskey from $ADODB_SESSION_TBL WHERE sesskey='$key'");
}
return !empty($rs);
}
function adodb_sess_destroy($key)
function adodb_sess_destroy($key)
{
global $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
if ($ADODB_SESSION_EXPIRE_NOTIFY) {
reset($ADODB_SESSION_EXPIRE_NOTIFY);
$fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
@@ -341,10 +343,10 @@ function adodb_sess_destroy($key)
return $rs ? true : false;
}
function adodb_sess_gc($maxlifetime)
function adodb_sess_gc($maxlifetime)
{
global $ADODB_SESS_DEBUG, $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
if ($ADODB_SESSION_EXPIRE_NOTIFY) {
reset($ADODB_SESSION_EXPIRE_NOTIFY);
$fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
@@ -362,21 +364,21 @@ function adodb_sess_gc($maxlifetime)
$rs->MoveNext();
}
$rs->Close();
$ADODB_SESS_CONN->CommitTrans();
}
} else {
$qry = "DELETE FROM $ADODB_SESSION_TBL WHERE expiry < " . time();
$ADODB_SESS_CONN->Execute($qry);
if ($ADODB_SESS_DEBUG) ADOConnection::outp("
-- <b>Garbage Collection</b>: $qry</p>");
}
// suggested by Cameron, "GaM3R" <gamr@outworld.cx>
if (defined('ADODB_SESSION_OPTIMIZE')) {
global $ADODB_SESSION_DRIVER;
switch( $ADODB_SESSION_DRIVER ) {
case 'mysql':
case 'mysqlt':
@@ -384,7 +386,7 @@ function adodb_sess_gc($maxlifetime)
break;
case 'postgresql':
case 'postgresql7':
$opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;
$opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;
break;
}
if (!empty($opt_qry)) {
@@ -393,29 +395,29 @@ function adodb_sess_gc($maxlifetime)
}
if ($ADODB_SESS_CONN->dataProvider === 'oci8') $sql = 'select TO_CHAR('.($ADODB_SESS_CONN->sysTimeStamp).', \'RRRR-MM-DD HH24:MI:SS\') from '. $ADODB_SESSION_TBL;
else $sql = 'select '.$ADODB_SESS_CONN->sysTimeStamp.' from '. $ADODB_SESSION_TBL;
$rs = $ADODB_SESS_CONN->SelectLimit($sql,1);
if ($rs && !$rs->EOF) {
$dbts = reset($rs->fields);
$rs->Close();
$dbt = $ADODB_SESS_CONN->UnixTimeStamp($dbts);
$t = time();
if (abs($dbt - $t) >= ADODB_SESSION_SYNCH_SECS) {
$msg =
$msg =
__FILE__.": Server time for webserver {$_SERVER['HTTP_HOST']} not in synch with database: database=$dbt ($dbts), webserver=$t (diff=".(abs($dbt-$t)/3600)." hrs)";
error_log($msg);
if ($ADODB_SESS_DEBUG) ADOConnection::outp("
-- $msg</p>");
}
}
return true;
}
session_module_name('user');
session_module_name('user');
session_set_save_handler(
"adodb_sess_open",
"adodb_sess_close",
@@ -435,5 +437,3 @@ if (0) {
ADOConnection::outp( "
-- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>",false);
}
?>
-1
View File
@@ -61,4 +61,3 @@ class MD5Crypt{
}
}
?>