3 # Copyright (C) 2006 Jason Woofenden
5 # This file is part of wfpl.
7 # wfpl is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # wfpl is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with wfpl; see the file COPYING. If not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 # you'll need this file that calles db_connect()
23 require_once('db_connect.php');
25 # and these database tables:
26 # create table sessions (id int unique auto_increment, session_key varchar(16), length int, expires int);
27 # create table session_data (id int unique auto_increment, session_id int, name varchar(100), value text);
31 # session_key 16 digit string identifying the session
32 # session_id integer id of the record in the "sessions" table of the database
33 # UNTIL_CLOSE a constant passed as session length to indicate "until browser window closes"
36 # session_id is kept in $GLOBALS
37 # session_key is sent as a cookie, and thus appears in $_REQUEST. The clean version is in $GLOBALS
39 # generate a new random 16-character string
40 function session_generate_key() {
41 $character_set = "abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWQYZ0123456789";
44 # PHP 4.2.0 and up seed the random number generator for you.
45 # Lets hope that it seeds with something harder to guess than the clock.
46 for($i = 0; $i < 16; ++$i) {
47 $id{$i} = $character_set{mt_rand(0, 61)};
53 # track this user with a session cookie (ie a cookie that goes away when the
54 # user closes the browser). The timestamp is how long to track the session in
55 # the database. Defaults to one day.
56 function session_new($length = 86400) {
57 $session_key = session_generate_key();
59 db_insert('sessions', 'session_key,length', $session_key, $length);
60 $GLOBALS['session_id'] = db_auto_id();
61 $GLOBALS['session_key'] = $session_key;
62 $_REQUEST['session_key'] = $session_key; #just in case someone calls session_exists() after session_new()
63 session_touch($length);
66 # call to renew the timeout for the session.
67 # assumes there's a session. call session_init() if you'd like one auto-create one if not found.
68 function session_touch($length = false) {
70 $length = db_get_value('sessions', 'length', 'id = %i', $GLOBALS['session_id']);
72 $expires = time() + $length;
74 header('Set-Cookie: session_key=' . $GLOBALS['session_key']);
76 db_update('sessions', 'expires', $expires, 'id = %i', $GLOBALS['session_id']);
79 # delete expired sessions from database
80 function session_purge_old() {
82 $exired_sessions = db_get_column('sessions', 'id', 'expires < %i', $now);
83 db_delete('sessions', 'expires < %i', $now);
84 if($expired_sessions) {
85 foreach($expired_sessions as $expired_session) {
86 db_delete('session_data', 'session_id=%i', $expired_session);
91 # return true if a session exists
92 function session_exists() {
93 if(!isset($_REQUEST['session_key'])) {
97 $session_key = ereg_replace('[^a-zA-Z0-9]', '', $_REQUEST['session_key']);
99 if(!strlen($session_key) == 16) {
103 $GLOBALS['session_key'] = $session_key;
106 $id = db_get_value('sessions', 'id', 'session_key = %"', $session_key);
111 $GLOBALS['session_id'] = $id;
115 # return username if a session exists and is authenticated
116 function session_exists_and_authed() {
117 if(!session_exists()) {
121 return session_get('auth_username');
125 # find existing session, or make one
126 function init_session() {
127 if(!session_exists()) {
132 # save a variable into the session
133 function session_set($name, $value) {
134 db_replace('session_data', 'name,value', $name, $value);
137 # get a variable into the session
138 function session_get($name) {
139 return db_get_value('session_data', 'value', 'name=%"', $name);