1 | <?php |
---|
2 | /* |
---|
3 | V4.93 10 Oct 2006 (c) 2000-2007 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. |
---|
8 | |
---|
9 | Latest version of ADODB is available at http://php.weblogs.com/adodb |
---|
10 | ====================================================================== |
---|
11 | |
---|
12 | This file provides PHP4 session management using the ADODB database |
---|
13 | wrapper library. |
---|
14 | |
---|
15 | Example |
---|
16 | ======= |
---|
17 | |
---|
18 | include('adodb.inc.php'); |
---|
19 | include('adodb-session.php'); |
---|
20 | session_start(); |
---|
21 | $_SESSION['AVAR'] += 1; |
---|
22 | print " |
---|
23 | -- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>"; |
---|
24 | |
---|
25 | To force non-persistent connections, call adodb_session_open first before session_start(): |
---|
26 | |
---|
27 | include('adodb.inc.php'); |
---|
28 | include('adodb-session.php'); |
---|
29 | adodb_sess_open(false,false,false); |
---|
30 | session_start(); |
---|
31 | $_SESSION['AVAR'] += 1; |
---|
32 | print " |
---|
33 | -- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>"; |
---|
34 | |
---|
35 | |
---|
36 | Installation |
---|
37 | ============ |
---|
38 | 1. Create this table in your database (syntax might vary depending on your db): |
---|
39 | |
---|
40 | create table sessions ( |
---|
41 | SESSKEY char(32) not null, |
---|
42 | EXPIRY int(11) unsigned not null, |
---|
43 | EXPIREREF varchar(64), |
---|
44 | DATA text not null, |
---|
45 | primary key (sesskey) |
---|
46 | ); |
---|
47 | |
---|
48 | For oracle: |
---|
49 | create table sessions ( |
---|
50 | SESSKEY char(32) not null, |
---|
51 | EXPIRY DECIMAL(16) not null, |
---|
52 | EXPIREREF varchar(64), |
---|
53 | DATA varchar(4000) not null, |
---|
54 | primary key (sesskey) |
---|
55 | ); |
---|
56 | |
---|
57 | |
---|
58 | 2. Then define the following parameters. You can either modify |
---|
59 | this file, or define them before this file is included: |
---|
60 | |
---|
61 | $ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase'; |
---|
62 | $ADODB_SESSION_CONNECT='server to connect to'; |
---|
63 | $ADODB_SESSION_USER ='user'; |
---|
64 | $ADODB_SESSION_PWD ='password'; |
---|
65 | $ADODB_SESSION_DB ='database'; |
---|
66 | $ADODB_SESSION_TBL = 'sessions' |
---|
67 | |
---|
68 | 3. Recommended is PHP 4.1.0 or later. There are documented |
---|
69 | session bugs in earlier versions of PHP. |
---|
70 | |
---|
71 | 4. If you want to receive notifications when a session expires, then |
---|
72 | you can tag a session with an EXPIREREF, and before the session |
---|
73 | record is deleted, we can call a function that will pass the EXPIREREF |
---|
74 | as the first parameter, and the session key as the second parameter. |
---|
75 | |
---|
76 | To do this, define a notification function, say NotifyFn: |
---|
77 | |
---|
78 | function NotifyFn($expireref, $sesskey) |
---|
79 | { |
---|
80 | } |
---|
81 | |
---|
82 | Then you need to define a global variable $ADODB_SESSION_EXPIRE_NOTIFY. |
---|
83 | This is an array with 2 elements, the first being the name of the variable |
---|
84 | you would like to store in the EXPIREREF field, and the 2nd is the |
---|
85 | notification function's name. |
---|
86 | |
---|
87 | In this example, we want to be notified when a user's session |
---|
88 | has expired, so we store the user id in the global variable $USERID, |
---|
89 | store this value in the EXPIREREF field: |
---|
90 | |
---|
91 | $ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn'); |
---|
92 | |
---|
93 | Then when the NotifyFn is called, we are passed the $USERID as the first |
---|
94 | parameter, eg. NotifyFn($userid, $sesskey). |
---|
95 | */ |
---|
96 | |
---|
97 | if (!defined('_ADODB_LAYER')) { |
---|
98 | include (dirname(__FILE__).'/adodb.inc.php'); |
---|
99 | } |
---|
100 | |
---|
101 | if (!defined('ADODB_SESSION')) { |
---|
102 | |
---|
103 | define('ADODB_SESSION',1); |
---|
104 | |
---|
105 | /* if database time and system time is difference is greater than this, then give warning */ |
---|
106 | define('ADODB_SESSION_SYNCH_SECS',60); |
---|
107 | |
---|
108 | /* |
---|
109 | Thanks Joe Li. See http://phplens.com/lens/lensforum/msgs.php?id=11487&x=1 |
---|
110 | */ |
---|
111 | function adodb_session_regenerate_id() |
---|
112 | { |
---|
113 | $conn =& ADODB_Session::_conn(); |
---|
114 | if (!$conn) return false; |
---|
115 | |
---|
116 | $old_id = session_id(); |
---|
117 | if (function_exists('session_regenerate_id')) { |
---|
118 | session_regenerate_id(); |
---|
119 | } else { |
---|
120 | session_id(md5(uniqid(rand(), true))); |
---|
121 | $ck = session_get_cookie_params(); |
---|
122 | setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure'],true); |
---|
123 | //@session_start(); |
---|
124 | } |
---|
125 | $new_id = session_id(); |
---|
126 | $ok =& $conn->Execute('UPDATE '. ADODB_Session::table(). ' SET sesskey='. $conn->qstr($new_id). ' WHERE sesskey='.$conn->qstr($old_id)); |
---|
127 | |
---|
128 | /* it is possible that the update statement fails due to a collision */ |
---|
129 | if (!$ok) { |
---|
130 | session_id($old_id); |
---|
131 | if (empty($ck)) $ck = session_get_cookie_params(); |
---|
132 | setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure'],true); |
---|
133 | return false; |
---|
134 | } |
---|
135 | |
---|
136 | return true; |
---|
137 | } |
---|
138 | |
---|
139 | /****************************************************************************************\ |
---|
140 | Global definitions |
---|
141 | \****************************************************************************************/ |
---|
142 | GLOBAL $ADODB_SESSION_CONNECT, |
---|
143 | $ADODB_SESSION_DRIVER, |
---|
144 | $ADODB_SESSION_USER, |
---|
145 | $ADODB_SESSION_PWD, |
---|
146 | $ADODB_SESSION_DB, |
---|
147 | $ADODB_SESS_CONN, |
---|
148 | $ADODB_SESS_LIFE, |
---|
149 | $ADODB_SESS_DEBUG, |
---|
150 | $ADODB_SESSION_EXPIRE_NOTIFY, |
---|
151 | $ADODB_SESSION_CRC, |
---|
152 | $ADODB_SESSION_TBL; |
---|
153 | |
---|
154 | |
---|
155 | $ADODB_SESS_LIFE = ini_get('session.gc_maxlifetime'); |
---|
156 | if ($ADODB_SESS_LIFE <= 1) { |
---|
157 | // bug in PHP 4.0.3 pl 1 -- how about other versions? |
---|
158 | //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $ADODB_SESS_LIFE</h3>"; |
---|
159 | $ADODB_SESS_LIFE=1440; |
---|
160 | } |
---|
161 | $ADODB_SESSION_CRC = false; |
---|
162 | //$ADODB_SESS_DEBUG = true; |
---|
163 | |
---|
164 | ////////////////////////////////// |
---|
165 | /* SET THE FOLLOWING PARAMETERS */ |
---|
166 | ////////////////////////////////// |
---|
167 | |
---|
168 | if (empty($ADODB_SESSION_DRIVER)) { |
---|
169 | $ADODB_SESSION_DRIVER='mysql'; |
---|
170 | $ADODB_SESSION_CONNECT='localhost'; |
---|
171 | $ADODB_SESSION_USER ='root'; |
---|
172 | $ADODB_SESSION_PWD =''; |
---|
173 | $ADODB_SESSION_DB ='xphplens_2'; |
---|
174 | } |
---|
175 | |
---|
176 | if (empty($ADODB_SESSION_EXPIRE_NOTIFY)) { |
---|
177 | $ADODB_SESSION_EXPIRE_NOTIFY = false; |
---|
178 | } |
---|
179 | // Made table name configurable - by David Johnson djohnson@inpro.net |
---|
180 | if (empty($ADODB_SESSION_TBL)){ |
---|
181 | $ADODB_SESSION_TBL = 'sessions'; |
---|
182 | } |
---|
183 | |
---|
184 | /* |
---|
185 | $ADODB_SESS['driver'] = $ADODB_SESSION_DRIVER; |
---|
186 | $ADODB_SESS['connect'] = $ADODB_SESSION_CONNECT; |
---|
187 | $ADODB_SESS['user'] = $ADODB_SESSION_USER; |
---|
188 | $ADODB_SESS['pwd'] = $ADODB_SESSION_PWD; |
---|
189 | $ADODB_SESS['db'] = $ADODB_SESSION_DB; |
---|
190 | $ADODB_SESS['life'] = $ADODB_SESS_LIFE; |
---|
191 | $ADODB_SESS['debug'] = $ADODB_SESS_DEBUG; |
---|
192 | |
---|
193 | $ADODB_SESS['debug'] = $ADODB_SESS_DEBUG; |
---|
194 | $ADODB_SESS['table'] = $ADODB_SESS_TBL; |
---|
195 | */ |
---|
196 | |
---|
197 | /****************************************************************************************\ |
---|
198 | Create the connection to the database. |
---|
199 | |
---|
200 | If $ADODB_SESS_CONN already exists, reuse that connection |
---|
201 | \****************************************************************************************/ |
---|
202 | function adodb_sess_open($save_path, $session_name,$persist=true) |
---|
203 | { |
---|
204 | GLOBAL $ADODB_SESS_CONN; |
---|
205 | if (isset($ADODB_SESS_CONN)) return true; |
---|
206 | |
---|
207 | GLOBAL $ADODB_SESSION_CONNECT, |
---|
208 | $ADODB_SESSION_DRIVER, |
---|
209 | $ADODB_SESSION_USER, |
---|
210 | $ADODB_SESSION_PWD, |
---|
211 | $ADODB_SESSION_DB, |
---|
212 | $ADODB_SESS_DEBUG; |
---|
213 | |
---|
214 | // cannot use & below - do not know why... |
---|
215 | $ADODB_SESS_CONN = ADONewConnection($ADODB_SESSION_DRIVER); |
---|
216 | if (!empty($ADODB_SESS_DEBUG)) { |
---|
217 | $ADODB_SESS_CONN->debug = true; |
---|
218 | ADOConnection::outp( " conn=$ADODB_SESSION_CONNECT user=$ADODB_SESSION_USER pwd=$ADODB_SESSION_PWD db=$ADODB_SESSION_DB "); |
---|
219 | } |
---|
220 | if ($persist) $ok = $ADODB_SESS_CONN->PConnect($ADODB_SESSION_CONNECT, |
---|
221 | $ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB); |
---|
222 | else $ok = $ADODB_SESS_CONN->Connect($ADODB_SESSION_CONNECT, |
---|
223 | $ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB); |
---|
224 | |
---|
225 | if (!$ok) ADOConnection::outp( " |
---|
226 | -- Session: connection failed</p>",false); |
---|
227 | } |
---|
228 | |
---|
229 | /****************************************************************************************\ |
---|
230 | Close the connection |
---|
231 | \****************************************************************************************/ |
---|
232 | function adodb_sess_close() |
---|
233 | { |
---|
234 | global $ADODB_SESS_CONN; |
---|
235 | |
---|
236 | if ($ADODB_SESS_CONN) $ADODB_SESS_CONN->Close(); |
---|
237 | return true; |
---|
238 | } |
---|
239 | |
---|
240 | /****************************************************************************************\ |
---|
241 | Slurp in the session variables and return the serialized string |
---|
242 | \****************************************************************************************/ |
---|
243 | function adodb_sess_read($key) |
---|
244 | { |
---|
245 | global $ADODB_SESS_CONN,$ADODB_SESSION_TBL,$ADODB_SESSION_CRC; |
---|
246 | |
---|
247 | $rs = $ADODB_SESS_CONN->Execute("SELECT data FROM $ADODB_SESSION_TBL WHERE sesskey = '$key' AND expiry >= " . time()); |
---|
248 | if ($rs) { |
---|
249 | if ($rs->EOF) { |
---|
250 | $v = ''; |
---|
251 | } else |
---|
252 | $v = rawurldecode(reset($rs->fields)); |
---|
253 | |
---|
254 | $rs->Close(); |
---|
255 | |
---|
256 | // new optimization adodb 2.1 |
---|
257 | $ADODB_SESSION_CRC = strlen($v).crc32($v); |
---|
258 | |
---|
259 | return $v; |
---|
260 | } |
---|
261 | |
---|
262 | return ''; // thx to Jorma Tuomainen, webmaster#wizactive.com |
---|
263 | } |
---|
264 | |
---|
265 | /****************************************************************************************\ |
---|
266 | Write the serialized data to a database. |
---|
267 | |
---|
268 | If the data has not been modified since adodb_sess_read(), we do not write. |
---|
269 | \****************************************************************************************/ |
---|
270 | function adodb_sess_write($key, $val) |
---|
271 | { |
---|
272 | global |
---|
273 | $ADODB_SESS_CONN, |
---|
274 | $ADODB_SESS_LIFE, |
---|
275 | $ADODB_SESSION_TBL, |
---|
276 | $ADODB_SESS_DEBUG, |
---|
277 | $ADODB_SESSION_CRC, |
---|
278 | $ADODB_SESSION_EXPIRE_NOTIFY; |
---|
279 | |
---|
280 | $expiry = time() + $ADODB_SESS_LIFE; |
---|
281 | |
---|
282 | // crc32 optimization since adodb 2.1 |
---|
283 | // now we only update expiry date, thx to sebastian thom in adodb 2.32 |
---|
284 | if ($ADODB_SESSION_CRC !== false && $ADODB_SESSION_CRC == strlen($val).crc32($val)) { |
---|
285 | if ($ADODB_SESS_DEBUG) echo " |
---|
286 | -- Session: Only updating date - crc32 not changed</p>"; |
---|
287 | $qry = "UPDATE $ADODB_SESSION_TBL SET expiry=$expiry WHERE sesskey='$key' AND expiry >= " . time(); |
---|
288 | $rs = $ADODB_SESS_CONN->Execute($qry); |
---|
289 | return true; |
---|
290 | } |
---|
291 | $val = rawurlencode($val); |
---|
292 | |
---|
293 | $arr = array('sesskey' => $key, 'expiry' => $expiry, 'data' => $val); |
---|
294 | if ($ADODB_SESSION_EXPIRE_NOTIFY) { |
---|
295 | $var = reset($ADODB_SESSION_EXPIRE_NOTIFY); |
---|
296 | global $$var; |
---|
297 | $arr['expireref'] = $$var; |
---|
298 | } |
---|
299 | $rs = $ADODB_SESS_CONN->Replace($ADODB_SESSION_TBL,$arr, |
---|
300 | 'sesskey',$autoQuote = true); |
---|
301 | |
---|
302 | if (!$rs) { |
---|
303 | ADOConnection::outp( ' |
---|
304 | -- Session Replace: '.$ADODB_SESS_CONN->ErrorMsg().'</p>',false); |
---|
305 | } else { |
---|
306 | // bug in access driver (could be odbc?) means that info is not commited |
---|
307 | // properly unless select statement executed in Win2000 |
---|
308 | if ($ADODB_SESS_CONN->databaseType == 'access') |
---|
309 | $rs = $ADODB_SESS_CONN->Execute("select sesskey from $ADODB_SESSION_TBL WHERE sesskey='$key'"); |
---|
310 | } |
---|
311 | return !empty($rs); |
---|
312 | } |
---|
313 | |
---|
314 | function adodb_sess_destroy($key) |
---|
315 | { |
---|
316 | global $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY; |
---|
317 | |
---|
318 | if ($ADODB_SESSION_EXPIRE_NOTIFY) { |
---|
319 | reset($ADODB_SESSION_EXPIRE_NOTIFY); |
---|
320 | $fn = next($ADODB_SESSION_EXPIRE_NOTIFY); |
---|
321 | $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM); |
---|
322 | $rs = $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE sesskey='$key'"); |
---|
323 | $ADODB_SESS_CONN->SetFetchMode($savem); |
---|
324 | if ($rs) { |
---|
325 | $ADODB_SESS_CONN->BeginTrans(); |
---|
326 | while (!$rs->EOF) { |
---|
327 | $ref = $rs->fields[0]; |
---|
328 | $key = $rs->fields[1]; |
---|
329 | $fn($ref,$key); |
---|
330 | $del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'"); |
---|
331 | $rs->MoveNext(); |
---|
332 | } |
---|
333 | $ADODB_SESS_CONN->CommitTrans(); |
---|
334 | } |
---|
335 | } else { |
---|
336 | $qry = "DELETE FROM $ADODB_SESSION_TBL WHERE sesskey = '$key'"; |
---|
337 | $rs = $ADODB_SESS_CONN->Execute($qry); |
---|
338 | } |
---|
339 | return $rs ? true : false; |
---|
340 | } |
---|
341 | |
---|
342 | function adodb_sess_gc($maxlifetime) |
---|
343 | { |
---|
344 | global $ADODB_SESS_DEBUG, $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY; |
---|
345 | |
---|
346 | if ($ADODB_SESSION_EXPIRE_NOTIFY) { |
---|
347 | reset($ADODB_SESSION_EXPIRE_NOTIFY); |
---|
348 | $fn = next($ADODB_SESSION_EXPIRE_NOTIFY); |
---|
349 | $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM); |
---|
350 | $t = time(); |
---|
351 | $rs =& $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE expiry < $t"); |
---|
352 | $ADODB_SESS_CONN->SetFetchMode($savem); |
---|
353 | if ($rs) { |
---|
354 | $ADODB_SESS_CONN->BeginTrans(); |
---|
355 | while (!$rs->EOF) { |
---|
356 | $ref = $rs->fields[0]; |
---|
357 | $key = $rs->fields[1]; |
---|
358 | $fn($ref,$key); |
---|
359 | $del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'"); |
---|
360 | $rs->MoveNext(); |
---|
361 | } |
---|
362 | $rs->Close(); |
---|
363 | |
---|
364 | $ADODB_SESS_CONN->CommitTrans(); |
---|
365 | |
---|
366 | } |
---|
367 | } else { |
---|
368 | $qry = "DELETE FROM $ADODB_SESSION_TBL WHERE expiry < " . time(); |
---|
369 | $ADODB_SESS_CONN->Execute($qry); |
---|
370 | |
---|
371 | if ($ADODB_SESS_DEBUG) ADOConnection::outp(" |
---|
372 | -- <b>Garbage Collection</b>: $qry</p>"); |
---|
373 | } |
---|
374 | // suggested by Cameron, "GaM3R" <gamr@outworld.cx> |
---|
375 | if (defined('ADODB_SESSION_OPTIMIZE')) { |
---|
376 | global $ADODB_SESSION_DRIVER; |
---|
377 | |
---|
378 | switch( $ADODB_SESSION_DRIVER ) { |
---|
379 | case 'mysql': |
---|
380 | case 'mysqlt': |
---|
381 | $opt_qry = 'OPTIMIZE TABLE '.$ADODB_SESSION_TBL; |
---|
382 | break; |
---|
383 | case 'postgresql': |
---|
384 | case 'postgresql7': |
---|
385 | $opt_qry = 'VACUUM '.$ADODB_SESSION_TBL; |
---|
386 | break; |
---|
387 | } |
---|
388 | if (!empty($opt_qry)) { |
---|
389 | $ADODB_SESS_CONN->Execute($opt_qry); |
---|
390 | } |
---|
391 | } |
---|
392 | if ($ADODB_SESS_CONN->dataProvider === 'oci8') $sql = 'select TO_CHAR('.($ADODB_SESS_CONN->sysTimeStamp).', \'RRRR-MM-DD HH24:MI:SS\') from '. $ADODB_SESSION_TBL; |
---|
393 | else $sql = 'select '.$ADODB_SESS_CONN->sysTimeStamp.' from '. $ADODB_SESSION_TBL; |
---|
394 | |
---|
395 | $rs =& $ADODB_SESS_CONN->SelectLimit($sql,1); |
---|
396 | if ($rs && !$rs->EOF) { |
---|
397 | |
---|
398 | $dbts = reset($rs->fields); |
---|
399 | $rs->Close(); |
---|
400 | $dbt = $ADODB_SESS_CONN->UnixTimeStamp($dbts); |
---|
401 | $t = time(); |
---|
402 | |
---|
403 | if (abs($dbt - $t) >= ADODB_SESSION_SYNCH_SECS) { |
---|
404 | |
---|
405 | $msg = |
---|
406 | __FILE__.": Server time for webserver {$_SERVER['HTTP_HOST']} not in synch with database: database=$dbt ($dbts), webserver=$t (diff=".(abs($dbt-$t)/3600)." hrs)"; |
---|
407 | error_log($msg); |
---|
408 | if ($ADODB_SESS_DEBUG) ADOConnection::outp(" |
---|
409 | -- $msg</p>"); |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | return true; |
---|
414 | } |
---|
415 | |
---|
416 | session_module_name('user'); |
---|
417 | session_set_save_handler( |
---|
418 | "adodb_sess_open", |
---|
419 | "adodb_sess_close", |
---|
420 | "adodb_sess_read", |
---|
421 | "adodb_sess_write", |
---|
422 | "adodb_sess_destroy", |
---|
423 | "adodb_sess_gc"); |
---|
424 | } |
---|
425 | |
---|
426 | /* TEST SCRIPT -- UNCOMMENT */ |
---|
427 | |
---|
428 | if (0) { |
---|
429 | |
---|
430 | session_start(); |
---|
431 | $_SESSION['AVAR'] += 1; |
---|
432 | ADOConnection::outp( " |
---|
433 | -- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>",false); |
---|
434 | } |
---|
435 | |
---|
436 | ?> |
---|