session_set_save_handler() problems.. [resolved]
Posted: Sun Jul 30, 2006 6:16 pm
Trying to use DB for session stuff and am starting to pull my hair out..
I've created this class, which is pretty much a like for like copy of Chris Shiflett's tutorial/article on session saving, but wrapped in a static class..
And the test script:
It just doesn't work. $_SESSION is always empty, no activity on the DB, no errors .. nothing.
Can anyone see anything that I'm missing?
I'm close to just calling it quits and completely omitting $_SESSION and creating my own session handler.
TIA
EDIT: found the problem.. my session table had sessionId as type INTEGER
I've created this class, which is pretty much a like for like copy of Chris Shiflett's tutorial/article on session saving, but wrapped in a static class..
Code: Select all
<?php
class jmt_Session
{
private static $db = array();
private static $link;
public static function set(array $db)
{
self::$db = $db;
}
public static function open ()
{
if (self::$link = mysql_connect(self::$db['HOST'], self::$db['USER'], self::$db['PASS'])) {
return mysql_select_db(self::$db['DATABASE'], self::$link);
} else {
return false;
}
}
public static function close ()
{
return mysql_close(self::$link);
}
public static function read ($id)
{
$id = mysql_real_escape_string($id, self::$link);
$sql = "SELECT `data` FROM `session` WHERE `sessionId` = '$id'";
if ($result = mysql_query($sql, self::$link)) {
list($data) = mysql_fetch_assoc($result);
return $data;
} else {
return '';
}
}
public static function write($id, $data)
{
$access = time();
$id = mysql_real_escape_string($id, self::$link);
$data = mysql_real_escape_string($data, self::$link);
$sql = "REPLACE
INTO `session` (`sessionId`, `access`, `data`)
VALUES ('$id', '$access', '$data')";
return mysql_query($sql, self::$link);
}
public static function destroy ($id)
{
$id = mysql_real_escape_string($id, self::$link);
$sql = "DELETE FROM `session` WHERE `sessionId` = '$id'";
return mysql_query($sql, self::$link);
}
public static function clean($max)
{
$old = time() - $max;
$sql = "DELETE
FROM `session`
WHERE `access` < '$old'";
return mysql_query($sql, self::$link);
}
}
?>Code: Select all
include_once 'includes/config/config.inc.php';
jmt_Session::set($CONFIG['DB']);
session_set_save_handler(
array('jmt_Session', 'open'),
array('jmt_Session', 'close'),
array('jmt_Session', 'read'),
array('jmt_Session', 'write'),
array('jmt_Session', 'destroy'),
array('jmt_Session', 'clean')
);
session_start();
var_dump($_SESSION);
$_SESSION['foo'] = 'bar';Can anyone see anything that I'm missing?
I'm close to just calling it quits and completely omitting $_SESSION and creating my own session handler.
TIA
EDIT: found the problem.. my session table had sessionId as type INTEGER