I'm thinking that maybe this just needs a fresh set of eyes, but this is my first go at using session_set_save_handler() and storing session data in mysql so who knows, maybe I've really messed something up.
I've written this up according to a few examples I found online and based on my needs. DB_sessions is a singleton I use for connecting to the db (I know this isn't what's broken, I use it all of the time).
When I run it I get Fatal error: Exception thrown without a stack frame in Unknown on line 0
. I think from what I'm seeing in my output page (bottom) and my database that the session variable is being written, but not via my session handler (because it echoes the session var, but the database is empty).
Here's my Session_handler class (I have some PDO error handling in there for now while it's not working)
<?php
if (! defined('EXT')){ exit('Invalid file request'); }
require_once('DB_sessions.php');
class Session_handler {
protected $db = '' ;
protected $life = '' ;
public function __construct() {
$this->life = get_cfg_var('session.gc_maxlifetime');
session_set_save_handler(
array($this, '_open'),
array($this, '_close'),
array($this, '_read'),
array($this, '_write'),
array($this, '_destroy'),
array($this, '_clean')
);
}
public function _open() {
$this->db = DB_sessions::get_instance();
return true ;
}
public function _close() {
$this->db = null ;
return true ;
}
public function _read($id) {
$expires = time();
$sth = $this->db->prepare('SELECT data FROM sessions WHERE id_sessions = :id_sessions AND expires > :expires');
$sth->bindParam(':id_sessions', $id, PDO::PARAM_STR);
$sth->bindParam(':expires', $expires, PDO::PARAM_STR);
if (!$sth->execute()) {
echo "\nPDO::errorInfo():\n";
print_r($sth->errorInfo());
exit('<br/>read') ;
}
$row = $sth->fetch(PDO::FETCH_OBJ);
$data = $row->data ;
return $data ;
}
function _write($id, $data) {
$expires = time() + $this->life ;
$sth = $this->db->prepare('REPLACE INTO sessions VALUES id_sessions = :id_sessions, expires = :expires, data = :data');
$sth->bindParam(':id_sessions', $id, PDO::PARAM_STR);
$sth->bindParam(':expires', $expires, PDO::PARAM_STR);
$sth->bindParam(':data', $data, PDO::PARAM_STR);
if (!$sth->execute()) {
echo "\nPDO::errorInfo():\n";
print_r($sth->errorInfo());
exit('<br/>write') ;
}
return true ;
}
function _destroy($id) {
$db = DB_sessions::get_instance();
$sth = $this->db->prepare('DELETE FROM sessions WHERE id_sessions = :id_sessions');
$sth->bindParam(':id_sessions', $id, PDO::PARAM_STR);
if (!$sth->execute()) {
echo "\nPDO::errorInfo():\n";
print_r($sth->errorInfo());
exit('<br/>destroy') ;
}
return true ;
}
public function _clean() {
$sth = $this->db->prepare('DELETE FROM sessions WHERE expires < UNIX_TIMESTAMP()');
if (!$sth->execute()) {
echo "\nPDO::errorInfo():\n";
print_r($sth->errorInfo());
exit('<br/>clean') ;
}
return true ;
}
}
?>
and here's the script I'm running it from to test it...
<?php
define('EXT', '.' . pathinfo(__FILE__, PATHINFO_EXTENSION));
require_once('lib/Session_handler.php');
$sess = new Session_handler ;
session_start();
$_SESSION['data'] = 'testing';
echo $_SESSION['data'];
?>
Can anyone see what I am doing wrong, or even maybe offer some ideas as to what may throw that error?
As always, any help is greatly appreciated. Thanks in advance. Oh ya, I'm running on php 5.2.17.