source: branches/1.1dev/lib/MySQLSessionHandler.inc.php

Last change on this file was 82, checked in by scdev, 18 years ago

Changed all usage of addslashes to mysql_real_escape_quotes

File size: 3.9 KB
Line 
1<?php
2/**
3 * MySQLSessionHandler.inc.php
4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information.
5 * @author  Quinn Comendant <quinn@strangecode.com>
6 * @version 1.1
7 * @since   1999
8 */
9
10 
11/*
12DROP TABLE IF EXISTS session_tbl;
13CREATE TABLE session_tbl (
14    session_id char(32) NOT NULL default '',
15    session_data mediumtext NOT NULL,
16    last_access timestamp(14) NOT NULL,
17    PRIMARY KEY (session_id),
18    KEY last_access (last_access)
19) TYPE=MyISAM;
20*/
21
22function mysqlSessionOpen($save_path, $sess_name)
23{
24    global $sess_mysql;
25
26    if (!isset($sess_mysql['dbh']) && (!isset($sess_mysql['hostname']) || !isset($sess_mysql['user']) || !isset($sess_mysql['password']) || !isset($sess_mysql['db']) || !isset($sess_mysql['table']))) {
27        trigger_error('mysqlSessionOpen(): Database session handling global variables not specified.', E_USER_WARNING); 
28    }
29
30    // Connect and select.
31    if (!$sess_mysql['dbh']) {
32        $sess_mysql['dbh'] = @mysql_connect($sess_mysql['hostname'], $sess_mysql['user'], $sess_mysql['password']);
33    }
34    @mysql_select_db($sess_mysql['db'], $sess_mysql['dbh']);
35   
36    // Check for errors
37    if (mysql_error($sess_mysql['dbh'])) {
38        trigger_error('mysqlSessionOpen(): Cannot connect to database. ' . mysql_error($sess_mysql['dbh']), E_USER_WARNING); 
39        return false; 
40    }
41    return true; 
42}
43
44function mysqlSessionClose()
45{
46    return true;
47}
48
49function mysqlSessionRead($session_id)
50{
51    global $sess_mysql;
52   
53    // Select the data belonging to session $session_id from the MySQL session table   
54    $qid = mysql_query("SELECT session_data FROM " . $sess_mysql['table'] . " WHERE session_id = '" . mysql_real_escape_string($session_id) . "'", $sess_mysql['dbh']);
55   
56    // Check for errors
57    if (mysql_error($sess_mysql['dbh'])) { 
58        trigger_error('mysqlSessionRead(): Failed to read sessions.' . mysql_error($sess_mysql['dbh']), E_USER_WARNING); 
59        return ''; 
60    }
61   
62    // Return the session data that was found
63    if (mysql_num_rows($qid) == 1) {
64        $row = mysql_fetch_row($qid);
65        return $row[0];
66    }
67   
68    // NOTICE: Output is expected to be an empty string always rather than 'false'.
69    return '';
70}
71
72function mysqlSessionWrite($session_id, $session_data)
73{
74    global $sess_mysql;
75   
76    // Write the serialized session data ($session_data) to the MySQL session table
77    mysql_query("REPLACE INTO " . $sess_mysql['table'] . "(session_id, session_data, last_access) VALUES ('" . mysql_real_escape_string($session_id) . "', '" . mysql_real_escape_string($session_data) . "', null)", $sess_mysql['dbh']);
78   
79    // Check for errors
80    if (mysql_error($sess_mysql['dbh'])) { 
81        trigger_error('mysqlSessionWrite(): Failed to write session. ' . mysql_error($sess_mysql['dbh']), E_USER_WARNING); 
82        return false; 
83    }
84       
85    return true;     
86}
87
88function mysqlSessionDestroy($session_id)
89{
90    global $sess_mysql;
91
92    // Delete from the MySQL table all data for the session $session_id
93    mysql_query("DELETE FROM " . $sess_mysql['table'] . " WHERE session_id = '" . mysql_real_escape_string($session_id) . "'", $sess_mysql['dbh']);
94           
95    // Check for errors
96    if (mysql_error($sess_mysql['dbh'])) { 
97        trigger_error('mysqlSessionDestroy(): Failed to delete old session. ' . mysql_error($sess_mysql['dbh']), E_USER_WARNING); 
98        return false; 
99    }
100
101    return true;     
102}
103
104function mysqlSessionGarbage($max_lifetime=4000)
105{
106    global $sess_mysql;
107
108    // Delete old values from the MySQL session table
109    $qid = mysql_query("DELETE FROM " . $sess_mysql['table'] . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime), $sess_mysql['dbh']);
110           
111    // Check for errors
112    if (mysql_error($sess_mysql['dbh'])) { 
113        trigger_error('mysqlSessionGarbage(): Failed to delete old session. ' . mysql_error($sess_mysql['dbh']), E_USER_WARNING); 
114        return false; 
115    }
116       
117    return true;     
118}
119
120
121
122?>
Note: See TracBrowser for help on using the repository browser.