docs-session.old.htm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>ADODB Old Session Management Manual</title>
  5. <meta http-equiv="Content-Type"
  6. content="text/html; charset=iso-8859-1">
  7. <style type="text/css">
  8. body, td {
  9. /*font-family: Arial, Helvetica, sans-serif;*/
  10. font-size: 11pt;
  11. }
  12. pre {
  13. font-size: 9pt;
  14. background-color: #EEEEEE; padding: .5em; margin: 0px;
  15. }
  16. .toplink {
  17. font-size: 8pt;
  18. }
  19. </style>
  20. </head>
  21. <body style="background-color: rgb(255, 255, 255);">
  22. <h3>ADODB Session Management Manual</h3>
  23. <p>v5.20.3 01-Jan-2016<br>
  24. &copy; 2000-2013 John Lim (jlim#natsoft.com)<br>
  25. &copy; 2014 Damien Regad, Mark Newnham and the ADOdb community</p>
  26. <p> <font size="1">This software is dual licensed using BSD-Style and
  27. LGPL. This means you can use it in compiled proprietary and commercial
  28. products. </font>
  29. <p>Useful ADOdb links: <a href="http://adodb.sourceforge.net/#download">Download</a>
  30. &nbsp; <a href="http://adodb.sourceforge.net/#docs">Other Docs</a>
  31. </p>
  32. <h3>Introduction</h3>
  33. <p>This documentation discusses the old adodb-session.php.
  34. Here is the <a href=docs-session.htm>new documentation</a> on the newer adodb-session2.php.
  35. <p> We store state information specific to a user or web client in
  36. session variables. These session variables persist throughout a
  37. session, as the user moves from page to page. </p>
  38. <p>To use session variables, call session_start() at the beginning of
  39. your web page, before your HTTP headers are sent. Then for every
  40. variable you want to keep alive for the duration of the session, call
  41. session_register($variable_name). By default, the session handler will
  42. keep track of the session by using a cookie. You can save objects or
  43. arrays in session variables also.
  44. </p>
  45. <p>The default method of storing sessions is to store it in a file.
  46. However if you have special needs such as you:
  47. </p>
  48. <ul>
  49. <li>Have multiple web servers that need to share session info</li>
  50. <li>Need to do special processing of each session</li>
  51. <li>Require notification when a session expires</li>
  52. </ul>
  53. <p>The ADOdb session handler provides you with the above
  54. additional capabilities by storing the session information as records
  55. in a database table that can be shared across multiple servers. </p>
  56. <p>These records will be garbage collected based on the php.ini [session] timeout settings.
  57. You can register a notification function to notify you when the record has expired and
  58. is about to be freed by the garbage collector.</p>
  59. <p><b>Important Upgrade Notice:</b> Since ADOdb 4.05, the session files
  60. have been moved to its own folder, adodb/session. This is a rewrite
  61. of the session code by Ross Smith. The old session code is in
  62. adodb/session/old. </p>
  63. <h4>ADOdb Session Handler Features</h4>
  64. <ul>
  65. <li>Ability to define a notification function that is called when a
  66. session expires. Typically
  67. used to detect session logout and release global resources. </li>
  68. <li>Optimization of database writes. We crc32 the session data and
  69. only perform an update
  70. to the session data if there is a data change. </li>
  71. <li>Support for large amounts of session data with CLOBs (see
  72. adodb-session-clob.php). Useful
  73. for Oracle. </li>
  74. <li>Support for encrypted session data, see
  75. adodb-cryptsession.php. Enabling encryption is simply a matter of
  76. including adodb-cryptsession.php instead of adodb-session.php. </li>
  77. </ul>
  78. <h3>Setup</h3>
  79. <p>There are 3 session management files that you can use:
  80. </p>
  81. <pre>adodb-session.php : The default<br>adodb-session-clob.php : Use this if you are storing DATA in clobs<br>adodb-cryptsession.php : Use this if you want to store encrypted session data in the database<br><br>
  82. </pre>
  83. <p><strong>Examples</strong>
  84. <p><pre>
  85. <font
  86. color="#004040"> include('adodb/adodb.inc.php');<br> <br><b> $ADODB_SESSION_DRIVER='mysql';<br> $ADODB_SESSION_CONNECT='localhost';<br> $ADODB_SESSION_USER ='scott';<br> $ADODB_SESSION_PWD ='tiger';<br> $ADODB_SESSION_DB ='sessiondb';</b><br> <br> <b>include('adodb/session/adodb-session.php');</b><br> session_start();<br> <br> #<br> # Test session vars, the following should increment on refresh<br> #<br> $_SESSION['AVAR'] += 1;<br> print "&lt;p&gt;\$_SESSION['AVAR']={$_SESSION['AVAR']}&lt;/p&gt;";<br></font></pre>
  87. <p>To force non-persistent connections, call adodb_session_open() first before session_start():
  88. <p>
  89. <pre>
  90. <font color="#004040"><br> include('adodb/adodb.inc.php');<br> <br><b> $ADODB_SESSION_DRIVER='mysql';<br> $ADODB_SESSION_CONNECT='localhost';<br> $ADODB_SESSION_USER ='scott';<br> $ADODB_SESSION_PWD ='tiger';<br> $ADODB_SESSION_DB ='sessiondb';</b><br> <br> <b>include('adodb/session/adodb-session.php');<br> adodb_sess_open(false,false,false);</b><br> session_start();<br> </font>
  91. </pre>
  92. <p> The 3rd parameter to adodb_sess_open($path, $sessname, $connectMode) sets the connection method. You can pass in the following:</p>
  93. <table width="50%" border="1">
  94. <tr>
  95. <td><b>$connectMode</b></td>
  96. <td><b>Connection Method</b></td>
  97. </tr>
  98. <tr>
  99. <td>true</td>
  100. <td><p>PConnect( )</p></td>
  101. </tr>
  102. <tr>
  103. <td>false</td>
  104. <td>Connect( )</td>
  105. </tr>
  106. <tr>
  107. <td>'N'</td>
  108. <td>NConnect( )</td>
  109. </tr>
  110. <tr>
  111. <td>'P'</td>
  112. <td>PConnect( )</td>
  113. </tr>
  114. <tr>
  115. <td>'C'</td>
  116. <td>Connect( )</td>
  117. </tr>
  118. </table>
  119. <p>To use a encrypted sessions, simply replace the file adodb-session.php:</p>
  120. <pre> <font
  121. color="#004040"><br> include('adodb/adodb.inc.php');<br> <br><b> $ADODB_SESSION_DRIVER='mysql';<br> $ADODB_SESSION_CONNECT='localhost';<br> $ADODB_SESSION_USER ='scott';<br> $ADODB_SESSION_PWD ='tiger';<br> $ADODB_SESSION_DB ='sessiondb';<br> <br> include('adodb/session/adodb-cryptsession.php');</b><br> session_start();</font><br>
  122. </pre>
  123. <p>And the same technique for adodb-session-clob.php:</p>
  124. <pre> <font
  125. color="#004040"><br> include('adodb/adodb.inc.php');<br> <br><b> $ADODB_SESSION_DRIVER='mysql';<br> $ADODB_SESSION_CONNECT='localhost';<br> $ADODB_SESSION_USER ='scott';<br> $ADODB_SESSION_PWD ='tiger';<br> $ADODB_SESSION_DB ='sessiondb';<br> <br> include('adodb/session/adodb-session-clob.php');</b><br> session_start();</font>
  126. </pre>
  127. <p>An alternative way to set persistant or non-persistent connections is to call the following function before session_start() is called.
  128. <pre>
  129. ADODB_Session::persist('P'); # 'C' for non-persistent connections
  130. </pre>
  131. <h4>Installation</h4>
  132. <p>1. Create this table in your database (MySQL syntax):
  133. <p><pre> <a
  134. name="sessiontab"></a> <font color="#004040">
  135. create table sessions (
  136. SESSKEY char(32) not null,
  137. EXPIRY int(11) unsigned not null,
  138. EXPIREREF varchar(64),
  139. DATA text not null,
  140. primary key (sesskey)
  141. );</font>
  142. </pre>
  143. <p>You may want to rename the 'data' field to 'session_data' as
  144. 'data' appears to be a reserved word for one or more of the following:
  145. <ul>
  146. <li> ANSI SQL
  147. <li> IBM DB2
  148. <li> MS SQL Server
  149. <li> Postgres
  150. <li> SAP
  151. </ul>
  152. <p>
  153. If you do, then execute:
  154. <pre>
  155. ADODB_Session::dataFieldName('session_data');
  156. </pre>
  157. <p> For the adodb-session-clob.php version, create this:
  158. <p> <pre>
  159. <font
  160. color="#004040"><br> create table sessions (<br> SESSKEY char(32) not null,<br> EXPIRY int(11) unsigned not null,<br> EXPIREREF varchar(64),<br> DATA CLOB,<br> primary key (sesskey)<br> );</font>
  161. </pre>
  162. <p>2. Then define the following parameters. You can either modify this file, or define them before this file is included:
  163. <pre> <font
  164. color="#004040"><br> $ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase';<br> $ADODB_SESSION_CONNECT='server to connect to';<br> $ADODB_SESSION_USER ='user';<br> $ADODB_SESSION_PWD ='password';<br> $ADODB_SESSION_DB ='database';<br> $ADODB_SESSION_TBL = 'sessions'; # setting this is optional<br> </font>
  165. </pre><p>
  166. When the session is created, $<b>ADODB_SESS_CONN</b> holds the connection object.<br> <br> 3. Recommended is PHP 4.0.6 or later. There are documented session bugs in earlier versions of PHP.
  167. <h3>Notifications</h3>
  168. <p>You can receive notification when your session is cleaned up by the session garbage collector or
  169. when you call session_destroy().
  170. <p>PHP's session extension will automatically run a special garbage collection function based on
  171. your php.ini session.cookie_lifetime and session.gc_probability settings. This will in turn call
  172. adodb's garbage collection function, which can be setup to do notification.
  173. <p>
  174. <pre>
  175. PHP Session --> ADOdb Session --> Find all recs --> Send --> Delete queued
  176. GC Function GC Function to be deleted notification records
  177. executed at called by for all recs
  178. random time Session Extension queued for deletion
  179. </pre>
  180. <p>When a session is created, we need to store a value in the session record (in the EXPIREREF field), typically
  181. the userid of the session. Later when the session has expired, just before the record is deleted,
  182. we reload the EXPIREREF field and call the notification function with the value of EXPIREREF, which
  183. is the userid of the person being logged off.
  184. <p>ADOdb uses a global variable $ADODB_SESSION_EXPIRE_NOTIFY that you must predefine before session
  185. start to store the notification configuration.
  186. $ADODB_SESSION_EXPIRE_NOTIFY is an array with 2 elements, the
  187. first being the name of the session variable you would like to store in
  188. the EXPIREREF field, and the 2nd is the notification function's name. </p>
  189. <p>For example, suppose we want to be notified when a user's session has expired,
  190. based on the userid. When the user logs in, we store the id in the global session variable
  191. $USERID. The function name is 'NotifyFn'.
  192. <p>
  193. So we define (before session_start() is called): </p>
  194. <pre> <font color="#004040">
  195. $ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');
  196. </font></pre>
  197. And when the NotifyFn is called (when the session expires), the
  198. $USERID is passed in as the first parameter, eg. NotifyFn($userid, $sesskey). The
  199. session key (which is the primary key of the record in the sessions
  200. table) is the 2nd parameter.
  201. <p> Here is an example of a Notification function that deletes some
  202. records in the database and temporary files: </p>
  203. <pre><font color="#004040">
  204. function NotifyFn($expireref, $sesskey)
  205. {
  206. global $ADODB_SESS_CONN; # the session connection object
  207. $user = $ADODB_SESS_CONN-&gt;qstr($expireref);
  208. $ADODB_SESS_CONN-&gt;Execute("delete from shopping_cart where user=$user");
  209. system("rm /work/tmpfiles/$expireref/*");
  210. }</font>
  211. </pre>
  212. <p> NOTE 1: If you have register_globals disabled in php.ini, then you
  213. will have to manually set the EXPIREREF. E.g. </p>
  214. <pre> <font color="#004040">
  215. $GLOBALS['USERID'] = GetUserID();
  216. $ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');</font>
  217. </pre>
  218. <p> NOTE 2: If you want to change the EXPIREREF after the session
  219. record has been created, you will need to modify any session variable
  220. to force a database record update.
  221. </p>
  222. <h4>Neat Notification Tricks</h4>
  223. <p><i>ExpireRef</i> normally holds the user id of the current session.
  224. </p>
  225. <p>1. You can then write a session monitor, scanning expireref to see
  226. who is currently logged on.
  227. </p>
  228. <p>2. If you delete the sessions record for a specific user, eg.
  229. </p>
  230. <pre>delete from sessions where expireref = '$USER'<br></pre>
  231. then the user is logged out. Useful for ejecting someone from a
  232. site.
  233. <p>3. You can scan the sessions table to ensure no user
  234. can be logged in twice. Useful for security reasons.
  235. </p>
  236. <h3>Using Oracle CLOBs</h3>
  237. <p>Suppose you are storing the DATA field in a CLOB:
  238. <pre><font color="#004040">
  239. CREATE TABLE sessions (
  240. SESSKEY VARCHAR(32) NOT NULL,
  241. EXPIRY NUMBER(16) NOT NULL,
  242. EXPIREREF VARCHAR(64),
  243. DATA CLOB,
  244. PRIMARY KEY (sesskey)
  245. );</font>
  246. </pre>
  247. <p>Then your PHP code could look like this:
  248. <pre>
  249. ADODB_SESSION_DRIVER='oci8';
  250. $ADODB_SESSION_CONNECT=$tnsname;
  251. $ADODB_SESSION_USER ='scott';
  252. $ADODB_SESSION_PWD = 'tiger';
  253. $ADODB_SESSION_DB ='';
  254. $ADODB_SESSION_USE_LOBS = 'clob';
  255. $ADODB_SESSION_TBL = 'sessions';
  256. $ADODB_SESS_DEBUG=0;
  257. include(ADODB_DIR.'/session/adodb-session.php');
  258. ADODB_Session::persist('P'); # use 'C' for non-persistent connects
  259. session_start();
  260. </pre>
  261. <p>Note that you can set persistance using ADODB_Session::persist('P').
  262. <h3>Compression/Encryption Schemes</h3>
  263. Since ADOdb 4.05, thanks to Ross Smith, multiple encryption and
  264. compression schemes are supported. Currently, supported are:
  265. <p>
  266. <pre> MD5Crypt (crypt.inc.php)<br> MCrypt<br> Secure (Horde's emulation of MCrypt, if MCrypt module is not available.)<br> GZip<br> BZip2<br></pre>
  267. <p>These are stackable. E.g.
  268. <p><pre>ADODB_Session::filter(new ADODB_Compress_Bzip2());<br>ADODB_Session::filter(new ADODB_Encrypt_MD5());<br></pre>
  269. will compress and then encrypt the record in the database.
  270. <h3>adodb_session_regenerate_id()</h3>
  271. <p>Dynamically change the current session id with a newly generated one and update database. Currently only
  272. works with cookies. Useful to improve security by reducing the risk of session-hijacking.
  273. See this article on <a href=http://shiflett.org/articles/session-fixation>Session Fixation</a> for more info
  274. on the theory behind this feature. Usage:
  275. <pre>
  276. $ADODB_SESSION_DRIVER='mysql';
  277. $ADODB_SESSION_CONNECT='localhost';
  278. $ADODB_SESSION_USER ='root';
  279. $ADODB_SESSION_PWD ='abc';
  280. $ADODB_SESSION_DB ='phplens';
  281. include('path/to/adodb/session/adodb-session.php');
  282. session_start();
  283. # Every 10 page loads, reset cookie for safety.
  284. # This is extremely simplistic example, better
  285. # to regenerate only when the user logs in or changes
  286. # user privilege levels.
  287. if ((rand()%10) == 0) adodb_session_regenerate_id();
  288. </pre>
  289. <p>This function calls session_regenerate_id() internally or simulates it if the function does not exist.
  290. <h3>Vacuum/Optimize Database</h3>
  291. <p>During session garbage collection, if postgresql is detected,
  292. ADOdb can be set to run VACUUM. If mysql is detected, then optimize database
  293. could be called.You can turn this on or off using:</p>
  294. <pre>$turnOn = true; # or false
  295. ADODB_Session::optimize($turnOn);
  296. </pre>
  297. <p>The default for optimization is it is disabled.</p>
  298. <h2>More Info</h2>
  299. <p>Also see the <a href="docs-adodb.htm">core ADOdb documentation</a>.
  300. </p>
  301. </body>
  302. </html>