3 <title>ADODB Session Management Manual
</title>
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1">
6 body,td {font-family:Arial,Helvetica,sans-serif;font-size:
11pt}
8 .toplink {font-size:
8pt}
11 <body bgcolor=
"#FFFFFF">
12 <h3>ADODB Session Management Manual
</h3>
14 V4.20
22 Feb
2004 (c)
2000-
2004 John Lim (jlim#natsoft.com.my)
15 <p> <font size=
1>This software is dual licensed using BSD-Style and LGPL. This
16 means you can use it in compiled proprietary and commercial products.
</font>
17 <p>Useful ADOdb links:
<a href=http://php.weblogs.com/adodb
>Download
</a> <a href=http://php.weblogs.com/adodb_manual
>Other Docs
</a>
21 We store state information specific to a user or web client in session variables. These session variables
22 persist throughout a session, as the user moves from page to page.
24 To use session variables, call session_start() at the beginning of your web page,
25 before your HTTP headers are sent. Then for every variable you want to keep alive
26 for the duration of the session, call session_register($variable_name). By default,
27 the session handler will keep track of the session by using a cookie. You can save objects
28 or arrays in session variables also.
29 <p>The default method of storing sessions is to store it in a file. However if
30 you have special needs such as you:
32 <li>Have multiple web servers that need to share session info
</li>
33 <li>Need to do special processing of each session
</li>
34 <li>Require notification when a session expires
</li>
36 <p>Then the ADOdb session handler provides you with the above additional capabilities
37 by storing the session information as records in a database table that can be
38 shared across multiple servers.
39 <p><b>Important Upgrade Notice:
</b> Since ADOdb
4.05, the session files have been moved to its own folder, adodb/session. This is a rewrite
40 of the session code by Ross Smith. The old session code is in adodb/session/old.
41 <h4>ADOdb Session Handler Features
</h4>
43 <li>Ability to define a notification function that is called when a session expires. Typically
44 used to detect session logout and release global resources.
45 <li>Optimization of database writes. We crc32 the session data and only perform an update
46 to the session data if there is a data change.
47 <li>Support for large amounts of session data with CLOBs (see adodb-session-clob.php). Useful
49 <li>Support for encrypted session data, see adodb-cryptsession.inc.php. Enabling encryption
50 is simply a matter of including adodb-cryptsession.inc.php instead of adodb-session.inc.php.
53 <p>There are
3 session management files that you can use:
55 adodb-session.php : The default
56 adodb-session-clob.php : Use this if you are storing DATA in clobs
57 adodb-cryptsession.php : Use this if you want to store encrypted session data in the database
59 <strong>Examples
</strong>
61 include('adodb/adodb.inc.php');
63 <b> $ADODB_SESSION_DRIVER='mysql';
64 $ADODB_SESSION_CONNECT='localhost';
65 $ADODB_SESSION_USER ='scott';
66 $ADODB_SESSION_PWD ='tiger';
67 $ADODB_SESSION_DB ='sessiondb';
</b>
69 <b>include('adodb/session/adodb-session.php');
</b>
73 # Test session vars, the following should increment on refresh
75 $_SESSION['AVAR'] +=
1;
76 print
"<p>\$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
78 To force non-persistent connections, call adodb_session_open first before session_start():
80 include('adodb/adodb.inc.php');
82 <b> $ADODB_SESSION_DRIVER='mysql';
83 $ADODB_SESSION_CONNECT='localhost';
84 $ADODB_SESSION_USER ='scott';
85 $ADODB_SESSION_PWD ='tiger';
86 $ADODB_SESSION_DB ='sessiondb';
</b>
88 <b>include('adodb/session/adodb-session.php');
89 adodb_sess_open(false,false,false);
</b>
92 To use a encrypted sessions, simply replace the file:
94 include('adodb/adodb.inc.php');
96 <b> $ADODB_SESSION_DRIVER='mysql';
97 $ADODB_SESSION_CONNECT='localhost';
98 $ADODB_SESSION_USER ='scott';
99 $ADODB_SESSION_PWD ='tiger';
100 $ADODB_SESSION_DB ='sessiondb';
102 include('adodb/session/adodb-cryptsession.php');
</b>
105 And the same technique for adodb-session-clob.php:
107 include('adodb/adodb.inc.php');
109 <b> $ADODB_SESSION_DRIVER='mysql';
110 $ADODB_SESSION_CONNECT='localhost';
111 $ADODB_SESSION_USER ='scott';
112 $ADODB_SESSION_PWD ='tiger';
113 $ADODB_SESSION_DB ='sessiondb';
115 include('adodb/session/adodb-session-clob.php');
</b>
118 <h4>Installation
</h4>
119 1. Create this table in your database (syntax might vary depending on your db):
120 <a name=sessiontab
></a> <font color=#
004040>
121 create table sessions (
122 SESSKEY char(
32) not null,
123 EXPIRY int(
11) unsigned not null,
124 EXPIREREF varchar(
64),
126 primary key (sesskey)
129 For the adodb-session-clob.php version, create this:
131 create table sessions (
132 SESSKEY char(
32) not null,
133 EXPIRY int(
11) unsigned not null,
134 EXPIREREF varchar(
64),
136 primary key (sesskey)
139 2. Then define the following parameters. You can either modify
140 this file, or define them before this file is included:
142 $ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase';
143 $ADODB_SESSION_CONNECT='server to connect to';
144 $ADODB_SESSION_USER ='user';
145 $ADODB_SESSION_PWD ='password';
146 $ADODB_SESSION_DB ='database';
147 $ADODB_SESSION_TBL = 'sessions'; # setting this is optional
149 When the session is created, $
<b>ADODB_SESS_CONN
</b> holds the connection object.
151 3. Recommended is PHP
4.0.6 or later. There are documented session bugs
152 in earlier versions of PHP.
155 <h3>Notifications
</h3>
156 <p>If you want to receive notification when a session expires, then
157 tag the session record with a
<a href=
"#sessiontab">EXPIREREF
</a> tag (see the
158 definition of the sessions table above). Before any session record is deleted,
159 ADOdb will call a notification function, passing in the EXPIREREF.
161 When a session is first created, we check a global variable $ADODB_SESSION_EXPIRE_NOTIFY.
162 This is an array with
2 elements, the first being the name of the session variable
163 you would like to store in the EXPIREREF field, and the
2nd is the
164 notification function's name.
166 Suppose we want to be notified when a user's session
167 has expired, based on the userid. The user id in the global session variable $USERID.
168 The function name is 'NotifyFn'. So we define:
169 <pre> <font color=#
004040>
170 $ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');
172 And when the NotifyFn is called (when the session expires), we pass the $USERID
173 as the first parameter, eg. NotifyFn($userid, $sesskey). The session key (which is
174 the primary key of the record in the sessions table) is the
2nd parameter.
176 Here is an example of a Notification function that deletes some records in the database
178 <pre><font color=#
004040>
179 function NotifyFn($expireref, $sesskey)
181 global $ADODB_SESS_CONN; # the session connection object
183 $user = $ADODB_SESS_CONN-
>qstr($expireref);
184 $ADODB_SESS_CONN-
>Execute(
"delete from shopping_cart where user=$user");
185 system(
"rm /work/tmpfiles/$expireref/*");
191 NOTE: If you want to change the EXPIREREF after the session record has been
192 created, you will need to modify any session variable to force a database
194 <h4>Compression/Encryption Schemes
</h4>
195 Since ADOdb
4.05, thanks to Ross Smith, multiple encryption and compression schemes are supported.
196 Currently, supported:
198 MD5Crypt (crypt.inc.php)
200 Secure (Horde's emulation of MCrypt, if MCrypt module is not available.)
204 These are stackable. E.g.
206 ADODB_Session::filter(new ADODB_Compress_Bzip2());
207 ADODB_Session::filter(new ADODB_Encrypt_MD5());
209 will compress and then encrypt the record in the database.
211 Also see the
<a href=docs-adodb.htm
>core ADOdb documentation
</a>.