3 V3.60 16 June 2003 (c) 2000-2003 John Lim (jlim@natsoft.com.my). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence.
7 Set tabs to 4 for best viewing.
9 Latest version of ADODB is available at http://php.weblogs.com/adodb
10 ======================================================================
12 This file provides PHP4 session management using the ADODB database
18 GLOBAL $HTTP_SESSION_VARS;
19 include('adodb.inc.php');
20 include('adodb-session.php');
22 session_register('AVAR');
23 $HTTP_SESSION_VARS['AVAR'] += 1;
24 print "<p>\$HTTP_SESSION_VARS['AVAR']={$HTTP_SESSION_VARS['AVAR']}</p>";
26 To force non-persistent connections, call adodb_session_open first before session_start():
28 GLOBAL $HTTP_SESSION_VARS;
29 include('adodb.inc.php');
30 include('adodb-session.php');
31 adodb_sess_open(false,false,false);
33 session_register('AVAR');
34 $HTTP_SESSION_VARS['AVAR'] += 1;
35 print "<p>\$HTTP_SESSION_VARS['AVAR']={$HTTP_SESSION_VARS['AVAR']}</p>";
40 1. Create this table in your database (syntax might vary depending on your db):
42 create table sessions (
43 SESSKEY char(32) not null,
44 EXPIRY int(11) unsigned not null,
45 EXPIREREF varchar(64),
51 2. Then define the following parameters in this file:
52 $ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase';
53 $ADODB_SESSION_CONNECT='server to connect to';
54 $ADODB_SESSION_USER ='user';
55 $ADODB_SESSION_PWD ='password';
56 $ADODB_SESSION_DB ='database';
57 $ADODB_SESSION_TBL = 'sessions'
59 3. Recommended is PHP 4.0.6 or later. There are documented
60 session bugs in earlier versions of PHP.
62 4. If you want to receive notifications when a session expires, then
63 you can tag a session with an EXPIREREF, and before the session
64 record is deleted, we can call a function that will pass the EXPIREREF
65 as the first parameter, and the session key as the second parameter.
67 To do this, define a notification function, say NotifyFn:
69 function NotifyFn($expireref, $sesskey)
73 Then define a global variable, with the first parameter being the
74 global variable you would like to store in the EXPIREREF field, and
75 the second is the function name.
77 In this example, we want to be notified when a user's session
78 has expired, so we store the user id in $USERID, and make this
79 the value stored in the EXPIREREF field:
81 $ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');
84 if (!defined('_ADODB_LAYER')) {
85 include (dirname(__FILE__
).'/adodb.inc.php');
88 if (!defined('ADODB_SESSION')) {
90 define('ADODB_SESSION',1);
92 /* if database time and system time is difference is greater than this, then give warning */
93 define('ADODB_SESSION_SYNCH_SECS',60);
95 /****************************************************************************************\
97 \****************************************************************************************/
98 GLOBAL $ADODB_SESSION_CONNECT,
99 $ADODB_SESSION_DRIVER,
106 $ADODB_SESSION_EXPIRE_NOTIFY,
110 $ADODB_SESS_LIFE = ini_get('session.gc_maxlifetime');
111 if ($ADODB_SESS_LIFE <= 1) {
112 // bug in PHP 4.0.3 pl 1 -- how about other versions?
113 //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $ADODB_SESS_LIFE</h3>";
114 $ADODB_SESS_LIFE=1440;
116 $ADODB_SESSION_CRC = false;
117 //$ADODB_SESS_DEBUG = true;
119 //////////////////////////////////
120 /* SET THE FOLLOWING PARAMETERS */
121 //////////////////////////////////
123 if (empty($ADODB_SESSION_DRIVER)) {
124 $ADODB_SESSION_DRIVER='mysql';
125 $ADODB_SESSION_CONNECT='localhost';
126 $ADODB_SESSION_USER ='root';
127 $ADODB_SESSION_PWD ='';
128 $ADODB_SESSION_DB ='xphplens_2';
131 if (empty($ADODB_SESSION_EXPIRE_NOTIFY)) {
132 $ADODB_SESSION_EXPIRE_NOTIFY = false;
134 // Made table name configurable - by David Johnson djohnson@inpro.net
135 if (empty($ADODB_SESSION_TBL)){
136 $ADODB_SESSION_TBL = 'sessions';
140 $ADODB_SESS['driver'] = $ADODB_SESSION_DRIVER;
141 $ADODB_SESS['connect'] = $ADODB_SESSION_CONNECT;
142 $ADODB_SESS['user'] = $ADODB_SESSION_USER;
143 $ADODB_SESS['pwd'] = $ADODB_SESSION_PWD;
144 $ADODB_SESS['db'] = $ADODB_SESSION_DB;
145 $ADODB_SESS['life'] = $ADODB_SESS_LIFE;
146 $ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
148 $ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
149 $ADODB_SESS['table'] = $ADODB_SESS_TBL;
152 /****************************************************************************************\
153 Create the connection to the database.
155 If $ADODB_SESS_CONN already exists, reuse that connection
156 \****************************************************************************************/
157 function adodb_sess_open($save_path, $session_name,$persist=true)
159 GLOBAL $ADODB_SESS_CONN;
160 if (isset($ADODB_SESS_CONN)) return true;
162 GLOBAL $ADODB_SESSION_CONNECT,
163 $ADODB_SESSION_DRIVER,
169 // cannot use & below - do not know why...
170 $ADODB_SESS_CONN = ADONewConnection($ADODB_SESSION_DRIVER);
171 if (!empty($ADODB_SESS_DEBUG)) {
172 $ADODB_SESS_CONN->debug
= true;
173 ADOConnection
::outp( " conn=$ADODB_SESSION_CONNECT user=$ADODB_SESSION_USER pwd=$ADODB_SESSION_PWD db=$ADODB_SESSION_DB ");
175 if ($persist) $ok = $ADODB_SESS_CONN->PConnect($ADODB_SESSION_CONNECT,
176 $ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
177 else $ok = $ADODB_SESS_CONN->Connect($ADODB_SESSION_CONNECT,
178 $ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
180 if (!$ok) ADOConnection
::outp( "<p>Session: connection failed</p>",false);
183 /****************************************************************************************\
185 \****************************************************************************************/
186 function adodb_sess_close()
188 global $ADODB_SESS_CONN;
190 if ($ADODB_SESS_CONN) $ADODB_SESS_CONN->Close();
194 /****************************************************************************************\
195 Slurp in the session variables and return the serialized string
196 \****************************************************************************************/
197 function adodb_sess_read($key)
199 global $ADODB_SESS_CONN,$ADODB_SESSION_TBL,$ADODB_SESSION_CRC;
201 $rs = $ADODB_SESS_CONN->Execute("SELECT data FROM $ADODB_SESSION_TBL WHERE sesskey = '$key' AND expiry >= " . time());
206 $v = rawurldecode(reset($rs->fields
));
210 // new optimization adodb 2.1
211 $ADODB_SESSION_CRC = strlen($v).crc32($v);
216 return ''; // thx to Jorma Tuomainen, webmaster#wizactive.com
219 /****************************************************************************************\
220 Write the serialized data to a database.
222 If the data has not been modified since adodb_sess_read(), we do not write.
223 \****************************************************************************************/
224 function adodb_sess_write($key, $val)
232 $ADODB_SESSION_EXPIRE_NOTIFY;
234 $expiry = time() +
$ADODB_SESS_LIFE;
236 // crc32 optimization since adodb 2.1
237 // now we only update expiry date, thx to sebastian thom in adodb 2.32
238 if ($ADODB_SESSION_CRC !== false && $ADODB_SESSION_CRC == strlen($val).crc32($val)) {
239 if ($ADODB_SESS_DEBUG) echo "<p>Session: Only updating date - crc32 not changed</p>";
240 $qry = "UPDATE $ADODB_SESSION_TBL SET expiry=$expiry WHERE sesskey='$key' AND expiry >= " . time();
241 $rs = $ADODB_SESS_CONN->Execute($qry);
244 $val = rawurlencode($val);
246 $arr = array('sesskey' => $key, 'expiry' => $expiry, 'data' => $val);
247 if ($ADODB_SESSION_EXPIRE_NOTIFY) {
248 $var = reset($ADODB_SESSION_EXPIRE_NOTIFY);
250 $arr['expireref'] = $
$var;
252 $rs = $ADODB_SESS_CONN->Replace($ADODB_SESSION_TBL,$arr,
253 'sesskey',$autoQuote = true);
256 ADOConnection
::outp( '<p>Session Replace: '.$ADODB_SESS_CONN->ErrorMsg().'</p>',false);
258 // bug in access driver (could be odbc?) means that info is not commited
259 // properly unless select statement executed in Win2000
260 if ($ADODB_SESS_CONN->databaseType
== 'access')
261 $rs = $ADODB_SESS_CONN->Execute("select sesskey from $ADODB_SESSION_TBL WHERE sesskey='$key'");
266 function adodb_sess_destroy($key)
268 global $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
270 if ($ADODB_SESSION_EXPIRE_NOTIFY) {
271 reset($ADODB_SESSION_EXPIRE_NOTIFY);
272 $fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
273 $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM
);
274 $rs = $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
275 $ADODB_SESS_CONN->SetFetchMode($savem);
277 $ADODB_SESS_CONN->BeginTrans();
279 $ref = $rs->fields
[0];
280 $key = $rs->fields
[1];
282 $del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
285 $ADODB_SESS_CONN->CommitTrans();
288 $qry = "DELETE FROM $ADODB_SESSION_TBL WHERE sesskey = '$key'";
289 $rs = $ADODB_SESS_CONN->Execute($qry);
291 return $rs ?
true : false;
294 function adodb_sess_gc($maxlifetime)
296 global $ADODB_SESS_DEBUG, $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
298 if ($ADODB_SESSION_EXPIRE_NOTIFY) {
299 reset($ADODB_SESSION_EXPIRE_NOTIFY);
300 $fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
301 $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM
);
302 $rs = $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE expiry < " . time());
303 $ADODB_SESS_CONN->SetFetchMode($savem);
305 $ADODB_SESS_CONN->BeginTrans();
307 $ref = $rs->fields
[0];
308 $key = $rs->fields
[1];
310 $del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
313 $ADODB_SESS_CONN->CommitTrans();
316 $qry = "DELETE FROM $ADODB_SESSION_TBL WHERE expiry < " . time();
317 $ADODB_SESS_CONN->Execute($qry);
319 if ($ADODB_SESS_DEBUG) ADOConnection
::outp("<p><b>Garbage Collection</b>: $qry</p>");
321 // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
322 if (defined('ADODB_SESSION_OPTIMIZE')) {
323 global $ADODB_SESSION_DRIVER;
325 switch( $ADODB_SESSION_DRIVER ) {
328 $opt_qry = 'OPTIMIZE TABLE '.$ADODB_SESSION_TBL;
332 $opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;
335 if (!empty($opt_qry)) {
336 $ADODB_SESS_CONN->Execute($opt_qry);
340 $rs = $ADODB_SESS_CONN->SelectLimit('select '.$ADODB_SESS_CONN->sysTimeStamp
.' from '. $ADODB_SESSION_TBL,1);
341 if ($rs && !$rs->EOF
) {
343 $dbt = reset($rs->fields
);
345 $dbt = $ADODB_SESS_CONN->UnixTimeStamp($dbt);
347 if (abs($dbt - $t) >= ADODB_SESSION_SYNCH_SECS
) {
348 global $HTTP_SERVER_VARS;
349 $msg = "adodb-session.php: Server time for webserver {$HTTP_SERVER_VARS['HTTP_HOST']} not in synch: database=$dbt, webserver=".$t;
351 if ($ADODB_SESS_DEBUG) ADOConnection
::outp("<p>$msg</p>");
358 session_module_name('user');
359 session_set_save_handler(
364 "adodb_sess_destroy",
368 /* TEST SCRIPT -- UNCOMMENT */
371 GLOBAL $HTTP_SESSION_VARS;
374 session_register('AVAR');
375 $HTTP_SESSION_VARS['AVAR'] +
= 1;
376 ADOConnection
::outp( "<p>\$HTTP_SESSION_VARS['AVAR']={$HTTP_SESSION_VARS['AVAR']}</p>",false);