3 # you'll need this file that calles db_connect()
4 require_once('db_connect.php');
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);
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"
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
20 # generate a new random 16-character string
21 function session_generate_key() {
22 $character_set = "abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWQYZ0123456789";
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)};
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();
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);
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) {
51 $length = db_get_value('sessions', 'length', 'id = %i', $GLOBALS['session_id']);
53 $expires = time() + $length;
55 header('Set-Cookie: session_key=' . $GLOBALS['session_key']);
57 db_update('sessions', 'expires', $expires, 'id = %i', $GLOBALS['session_id']);
60 # delete expired sessions from database
61 function session_purge_old() {
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);
72 # return true if a session exists
73 function session_exists() {
74 if(!isset($_REQUEST['session_key'])) {
78 $session_key = ereg_replace('[^a-zA-Z0-9]', '', $_REQUEST['session_key']);
80 if(!strlen($session_key) == 16) {
84 $GLOBALS['session_key'] = $session_key;
87 $id = db_get_value('sessions', 'id', 'session_key = %"', $session_key);
92 $GLOBALS['session_id'] = $id;
96 # return username if a session exists and is authenticated
97 function session_exists_and_authed() {
98 if(!session_exists()) {
102 return session_get('auth_username');
106 # find existing session, or make one
107 function init_session() {
108 if(!session_exists()) {
113 # save a variable into the session
114 function session_set($name, $value) {
115 db_replace('session_data', 'name,value', $name, $value);
118 # get a variable into the session
119 function session_get($name) {
120 return db_get_value('session_data', 'value', 'name=%"', $name);