Log in with username/password: lyceum/lyceum

root/branches/1.0/src/lib/session.php

Revision 1273, 2.1 kB (checked in by jjb, 2 years ago)

#885, #729

  • Sessions last for 10 years, and sessions are not "garbage collected" for 10 years.
  • The user may specify a custom path for session data to be stored
  • The user may turn on the experimental DB session storage system

There is a jumble of mediocre documentation about the various dimensions in php sessions-- the consensus seems to be that session data might be "garbage collected" before the session data expires. I cannot imagine what "garbage collection" could possibly mean in this case-- in call other cases in computer science it means data that the program is absolutely certain is not being used by some part of the system. Here it seems to be a simplistic notion of time-based expiration.

Theses were helpful:
http://www.captain.at/howto-php-sessions.php
http://www.php.net/manual/en/ref.session.php#68537

Line 
1 <?php
2
3 if (defined('DBSESSIONS'))
4     session_set_save_handler( '_open',
5                               '_close',
6                               '_read',
7                               '_write',
8                               '_destroy',
9                               '_clean' );
10
11 function _open()
12 {   
13     global $_sess_db, $wpdb;
14 //trace('opening session', "...");
15     if ($_sess_db = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD))
16     {
17         return mysql_select_db(DB_NAME, $_sess_db);
18     }
19 //trace('FAILURE-- DB_HOST, DB_USER, DB_PASSWORD', DB_HOST . DB_USER . DB_PASSWORD);
20     return FALSE;
21 }
22
23 function _close()
24 {
25     global $_sess_db, $wpdb;
26 //trace('closing session', "...");
27     return mysql_close($_sess_db);
28 }
29
30 function _read($id)
31 {
32     global $_sess_db, $wpdb;
33 //trace('reading session data for $id', "$id");
34     $id = mysql_real_escape_string($id);
35
36     $sql = "SELECT data
37             FROM   $wpdb->sessions
38             WHERE  id = '$id'";
39
40     if ($result = mysql_query($sql, $_sess_db))
41     {
42         if (mysql_num_rows($result))
43         {
44             $record = mysql_fetch_assoc($result);
45
46             return $record['data'];
47         }
48     }
49
50     return '';
51 }
52
53 function _write($id, $data)
54 {   
55     global $_sess_db, $wpdb;
56     $access = time();
57
58     $id = mysql_real_escape_string($id);
59     $access = mysql_real_escape_string($access);
60     $data = mysql_real_escape_string($data);
61
62     //trace('writing session $id, $data', "$id, $data");
63     $sql = "REPLACE
64             INTO    $wpdb->sessions
65             VALUES  ('$id', '$access', '$data')";
66 //trace('$sql', "$sql");
67     return mysql_query($sql, $_sess_db);
68 }
69
70 function _destroy($id)
71 {
72     global $_sess_db, $wpdb;
73 trace('destroying session data for $id', "$id");   
74     $id = mysql_real_escape_string($id);
75
76     $sql = "DELETE
77             FROM   $wpdb->sessions
78             WHERE  id = '$id'";
79
80     return mysql_query($sql, $_sess_db);
81 }
82
83 function _clean($max)
84 {
85     global $_sess_db, $wpdb;
86 //trace('cleaning session data for $max', "$max");
87     $old = time() - $max;
88     $old = mysql_real_escape_string($old);
89
90     $sql = "DELETE
91             FROM   $wpdb->sessions
92             WHERE  access < '$old'";
93
94     return mysql_query($sql, $_sess_db);
95 }
96
97 ?>
Note: See TracBrowser for help on using the browser.
Log in with username/password: lyceum/lyceum