ona  18.1.1
About: OpenNetAdmin provides a database managed inventory of your IP network (with Web and CLI interface).
  Fossies Dox: ona-18.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

adodb_sessions.inc.php
Go to the documentation of this file.
1 <?php
2 /* ------------------------------------------------------------------------
3  * session_mysql.php
4  * ------------------------------------------------------------------------
5  * PHP4 MySQL Session Handler
6  * Version 1.22 - ADODB
7  * by Ying Zhang (ying@zippydesign.com)
8  * Last Modified: 2006-11-18 (by Brandon Zehm)
9  * Now uses db_delete_records() for sess_gc()
10  * Now requires ADODB but should require fewer db connections.
11  * ------------------------------------------------------------------------
12  * TERMS OF USAGE:
13  * ------------------------------------------------------------------------
14  * You are free to use this library in any way you want, no warranties are
15  * expressed or implied. This works for me, but I don't guarantee that it
16  * works for you, USE AT YOUR OWN RISK.
17  *
18  * ------------------------------------------------------------------------
19  * DESCRIPTION:
20  * ------------------------------------------------------------------------
21  * This library tells the PHP4 session handler to write to a MySQL database
22  * instead of creating individual files for each session.
23  *
24  * Create a new database in MySQL called "sessions" like so:
25  *
26  * CREATE TABLE sessions (
27  * sesskey char(32) not null,
28  * expiry int(11) unsigned not null,
29  * sessvalue text not null,
30  * PRIMARY KEY (sesskey)
31  * );
32  *
33  * ------------------------------------------------------------------------
34  * INSTALLATION:
35  * ------------------------------------------------------------------------
36  * Make sure you have MySQL support compiled into PHP. Then copy this
37  * script to a directory that is accessible by the rest of your PHP
38  * scripts.
39  *
40  * ------------------------------------------------------------------------
41  * USAGE:
42  * ------------------------------------------------------------------------
43  * Include this file in your scripts before you call session_start(), you
44  * don't have to do anything special after that.
45  *
46  * ------------------------------------------------------------------------
47  * DEVELOPERS:
48  * ------------------------------------------------------------------------
49  * These SQL queries do not need mysql_escape_string in them, PHP
50  * does that sort of thing before it passes data to these functions.
51 */
52 
55 $SESS_LIFE = $conf['cookie_life'];
56 
57 // The following is a fix/workaround for php 5.2 and the fact
58 // that it handles destructors in a new order.
59 register_shutdown_function('session_write_close');
60 
61 function sess_open($save_path, $session_name) { return true; }
62 function sess_close() { return true; }
63 
64 
65 // Reads and returns a session's data
66 function sess_read($key) {
67  global $SESS_DBH, $SESS_LIFE;
68  printmsg("sess_read($key) called", 6);
69 
70  list($status, $rows, $record) = db_get_record($SESS_DBH, 'sessions', "`sesskey` = '$key' AND `expiry` > " . time());
71  if ($status or $rows == 0) { return false; }
72 
73  if (array_key_exists('sessvalue', $record)) {
74  // Update the expiry time (i.e. keep sessions alive even if nothing in the session has changed)
75  $expiry = time() + $SESS_LIFE;
76  list($status, $rows) = db_update_record($SESS_DBH, 'sessions', "`sesskey` = '$key' AND `expiry` > " . time(), array('expiry' => $expiry));
77  if ($status) { return false; }
78 
79  // Return the value
80  return($record['sessvalue']);
81  }
82 
83  return false;
84 }
85 
86 
87 function sess_write($key, $value) {
88  global $SESS_DBH, $SESS_LIFE;
89  //printmsg("sess_write($key, $value) called", 6);
90 
91  $expiry = time() + $SESS_LIFE;
92 
93  // Try inserting the value into the DB
94  list($status, $rows) = db_insert_record($SESS_DBH, 'sessions', array('sesskey' => $key, 'expiry' => $expiry, 'sessvalue' => $value));
95 
96  // If the insert failed try an update
97  if (!$status or $rows == 0) {
98  list($status, $rows) = db_update_record($SESS_DBH, 'sessions', array('sesskey' => $key), array('expiry' => $expiry, 'sessvalue' => $value));
99  }
100 
101  return true;
102 }
103 
104 
105 function sess_destroy($key) {
106  global $SESS_DBH;
107  list($status, $rows) = db_delete_records($SESS_DBH, 'sessions', array('sesskey' => $key));
108  return true;
109 }
110 
111 
112 function sess_gc($lifetime) {
113  global $SESS_DBH;
114  list($status, $rows) = db_delete_records($SESS_DBH, "`sessions`", "`expiry` < " . time());
115  return true;
116 }
117 
118 
119 session_set_save_handler(
120  "sess_open",
121  "sess_close",
122  "sess_read",
123  "sess_write",
124  "sess_destroy",
125  "sess_gc"
126 );
127 
128 // DON'T put whitespace at the beginning or end of this file!!!
129 ?>
db_insert_record
db_insert_record($dbh=0, $table="", $insert="")
Definition: functions_db.inc.php:375
sess_write
sess_write($key, $value)
Definition: adodb_sessions.inc.php:87
$record
$record['display_name']
Definition: app_advanced_search.inc.php:12
$status
$status
Definition: install.php:12
sess_open
sess_open($save_path, $session_name)
Definition: adodb_sessions.inc.php:61
db_delete_records
db_delete_records($dbh=0, $table="", $where="")
Definition: functions_db.inc.php:582
sess_gc
sess_gc($lifetime)
Definition: adodb_sessions.inc.php:112
sess_close
sess_close()
Definition: adodb_sessions.inc.php:62
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
db_update_record
db_update_record($dbh=0, $table="", $where="", $insert="")
Definition: functions_db.inc.php:474
sess_read
sess_read($key)
Definition: adodb_sessions.inc.php:66
$onadb
global $onadb
Definition: adodb_sessions.inc.php:53
sess_destroy
sess_destroy($key)
Definition: adodb_sessions.inc.php:105
$SESS_DBH
global $SESS_DBH
Definition: adodb_sessions.inc.php:53
$conf
global $conf
Definition: adodb_sessions.inc.php:53
db_get_record
db_get_record($dbh=0, $table="", $where="", $order="")
Definition: functions_db.inc.php:708
$SESS_LIFE
global $SESS_LIFE
Definition: adodb_sessions.inc.php:53