JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
major updates to db.php, added session.php, run.php uses file_run.php
[wfpl.git] / session.php
1 <?php
2
3 # you'll need this file that calles db_connect()
4 require_once('db_connect.php');
5
6 # and these database tables:
7 # create table sessions (id int unique auto_increment, session_key varchar(16), length int, expires int);
8 # create table session_data (id int unique auto_increment, session_id int, name varchar(100), value text);
9
10 # GLOSSARY
11 #
12 # session_key  16 digit string identifying the session
13 # session_id   integer id of the record in the "sessions" table of the database
14 # UNTIL_CLOSE  a constant passed as session length to indicate "until browser window closes"
15
16
17 # session_id is kept in $GLOBALS
18 # session_key is sent as a cookie, and thus appears in $_REQUEST. The clean version is in $GLOBALS
19
20 # generate a new random 16-character string
21 function session_generate_key() {
22         $character_set = "abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWQYZ0123456789";
23     $id = "                ";
24
25         # PHP 4.2.0 and up seed the random number generator for you.
26         # Lets hope that it seeds with something harder to guess than the clock.
27     for($i = 0; $i < 16; ++$i) {
28         $id{$i} = $character_set{mt_rand(0, 61)};
29     }
30
31     return $id;
32 }
33
34 # track this user with a session cookie (ie a cookie that goes away when the
35 # user closes the browser). The timestamp is how long to track the session in
36 # the database. Defaults to one day.
37 function session_new($length = 86400) {
38         $session_key = session_generate_key();
39
40         db_insert('sessions', 'session_key,length', $session_key, $length);
41         $GLOBALS['session_id'] = db_auto_id();
42         $GLOBALS['session_key'] = $session_key;
43         $_REQUEST['session_key'] = $session_key; #just in case someone calls session_exists() after session_new()
44         session_touch($length);
45 }
46
47 # call to renew the timeout for the session.
48 # assumes there's a session. call session_init() if you'd like one auto-create one if not found.
49 function session_touch($length = false) {
50         if(!$length) {
51                 $length = db_get_value('sessions', 'length', 'id = %i', $GLOBALS['session_id']);
52         }
53         $expires = time() + $length;
54
55         header('Set-Cookie: session_key=' . $GLOBALS['session_key']);
56
57         db_update('sessions', 'expires', $expires, 'id = %i', $GLOBALS['session_id']);
58 }
59
60 # delete expired sessions from database
61 function session_purge_old() {
62         $now = time();
63         $exired_sessions = db_get_column('sessions', 'id', 'expires < %i', $now);
64         db_delete('sessions', 'expires < %i', $now);
65         if($expired_sessions) {
66                 foreach($expired_sessions as $expired_session) {
67                         db_delete('session_data', 'session_id=%i', $expired_session);
68                 }
69         }
70 }
71
72 # return true if a session exists
73 function session_exists() {
74         if(!isset($_REQUEST['session_key'])) {
75                 return false;
76         }
77
78         $session_key = ereg_replace('[^a-zA-Z0-9]', '', $_REQUEST['session_key']);
79
80         if(!strlen($session_key) == 16) {
81                 return false;
82         }
83
84         $GLOBALS['session_key'] = $session_key;
85
86         session_purge_old();
87         $id = db_get_value('sessions', 'id', 'session_key = %"', $session_key);
88         if($id === false) {
89                 return false;
90         }
91
92         $GLOBALS['session_id'] = $id;
93         return true;
94 }
95
96 # return username if a session exists and is authenticated
97 function session_exists_and_authed() {
98         if(!session_exists()) {
99                 return false;
100         }
101
102         return session_get('auth_username');
103 }
104
105
106 # find existing session, or make one
107 function init_session() {
108         if(!session_exists()) {
109                 session_new();
110         }
111 }
112
113 # save a variable into the session
114 function session_set($name, $value) {
115         db_replace('session_data', 'name,value', $name, $value);
116 }
117
118 # get a variable into the session
119 function session_get($name) {
120         return db_get_value('session_data', 'value', 'name=%"', $name);
121 }
122
123 ?>