I've spent probably 4 hours trying to figure this out. It's probably something really dumb, but I just can't see it. I am having a problem with the session_set_save_handler pulling out session data on multiple pages. Here's the session class:
<?
/*
* Session handling class
* Instantiate with db host, user, pass, db, table, field sess id, field expiration, field value
*/
class Sessions {
var $ttl;
var $sqlhost;
var $sqluser;
var $sqlpass;
var $sqldb;
var $sqltable;
var $tblsid;
var $tblexpire;
var $tblvalue;
/*
* Construct the class
*/
function Sessions($ttl, $sqlhost, $sqluser, $sqlpass, $sqldb, $sqltable, $tblsid, $tblexpire, $tblvalue) {
$this->ttl = $ttl;
$this->sqlhost = $sqlhost;
$this->sqluser = $sqluser;
$this->sqlpass = $sqlpass;
$this->sqldb = $sqldb;
$this->sqltable = $sqltable;
$this->tblsid = $tblsid;
$this->tblexpire = $tblexpire;
$this->tblvalue = $tblvalue;
}
/*
* mysql_session_open()
* Opens a persistent server connection and selects the database
*/
function mysql_session_open($session_path, $session_name) {
mysql_pconnect($this->sqlhost, $this->sqluser, $this->sqlpass)
or die('Could not connect to MySQL database. ' . mysql_error());
mysql_select_db($this->sqldb);
}
/*
* mysql_session_close()
* Doesn't really do anything, but we need it to return true
*/
function mysql_session_close() {
return 1;
}
/*
* mysql_session_select()
* Reads the session data from the database
*/
function mysql_session_select($sid) {
$sql = "SELECT " . $this->tblvalue . " FROM " . $this->sqltable .
" WHERE " . $this->tblsid . " = '$sid' AND " .
$this->tblexpire . " > ". time();
$result = mysql_query($sql);
if (mysql_num_rows($result)) {
$row=mysql_fetch_assoc($result);
$value = $row['value'];
return $value;
} else {
return "";
}
}
/*
* mysql_session_write()
* This function writes the session data to the database.
* If that SID already exists, then the existing data will be updated.
* NOTE: We can set $lifetime to whatever we want to because the expiry
* date is being stored in the database.
*/
function mysql_session_write($sid, $value) {
/*
* We could use this to set the expiry to the maximum allowed by PHP
* but instead we let this be set when we instantiate this class
* $lifetime = get_cfg_var("session.gc_maxlifetime");
*/
$lifetime = $this->ttl;
$expiration = time() + $lifetime;
$sql = "INSERT INTO " . $this->sqltable .
" VALUES('$sid', '$expiration', '$value')";
$result = mysql_query($sql);
if (! $result) {
$sql = "UPDATE " . $this->sqltable . " SET " .
$this->tblexpire . " = '$expiration', " .
addslashes($this->tblvalue) . " = '$value' WHERE " .
$this->tblsid . " = '$sid' AND " . $this->tblexpire . " >". time();
$result = mysql_query($sql);
}
}
/*
* mysql_session_destroy()
* Deletes all session information having input sid (only 1 row)
*/
function mysql_session_destroy($sid) {
$sql = "DELETE FROM " . $this->sqltable .
" WHERE " . $this->tblsid . "= '$sid'";
$result = mysql_query($sql);
}
/*
* mysql_session_garbage_collect()
* Deletes all sessions that have expired.
*/
function mysql_session_garbage_collect($lifetime) {
$sql = "DELETE FROM " . $this->sqltable .
" WHERE " . $this->tblexpire . " < ".time() - $lifetime;
$result = mysql_query($sql);
return mysql_affected_rows($result);
}
/*
* Tie this all into PHP's handler logic
*/
function session_set() {
session_set_save_handler(
array(&$this, "mysql_session_open"),
array(&$this, "mysql_session_close"),
array(&$this, "mysql_session_select"),
array(&$this, "mysql_session_write"),
array(&$this, "mysql_session_destroy"),
array(&$this, "mysql_session_garbage_collect"));
}
}
?>
Here's the first page that instantiates the class and registers a session variable.
<?
define("SQLHOST", "localhost");
define("SQLUSER", "root");
define("SQLPASS", "pass");
define("SQLDB", "database");
define("SESSTTL", "1440");
include("./classes/sessions.class.php");
$newsess = new Sessions(SESSTTL, SQLHOST, SQLUSER, SQLPASS, SQLDB, sessions, s_id, s_expire, s_value);
$newsess->session_set();
session_start();
$_SESSION['sess_uid'] = "User ID";
echo "Your user id is: " . $_SESSION['sess_uid'];
echo "<br>Go to page <a href=\"page2.php\">2</a>";
?>
This page outputs the following:
Your user id is: User ID
Go to page 2
This is all good so far, until you go to page 2. Here is the code for page 2:
<?
session_start();
echo "Your user id is: " . $_SESSION['sess_uid'];
?>
Page 2 outputs the following:
Your user id is:
If I watch the MySQL table, after page 1 is executed, the variables show up perfectly, after page 2 is loaded, the session exists, but the variables are gone. I can't get the session to propagate over multiple pages and I have no idea why. Any help is much appreciated, thanks in advance!!