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!!

    I haven't really looked at your code in detail.

    I just noticed your last sentence about it not following over to the next page.

    Can you verify that the session is the same for both page 1 and 2?

    Are you positive it didn't make a new session on page 2?

    I've had this problem in IE before where going to the next page would make IE register a new session, as if it was two different browsers browsing.

    Just checking before I read your code in detail.

      Well this is odd. When I load the first page, it creates the session in mysql

      6c5a2efb0b3471f3a143cce54633d988 1107022930 sess_uid|s:7:"User ID";

      When I load the second page, it would just empty the last part starting with sess_uid. It no longer does that when the second page is loaded, but the second page still does not output the session sess_uid. I'm guessing it is treating it as a new session and writing it to the /tmp directory for the second page and referencing that.. but it was to my understanding that once you use register the save_handler function you only need to start the rest of the pages with session_start();

      I'm pretty certain I'm doing something wrong,.. I just have no idea what.

        Any ideas how I can get the session initiated in the first page to work on the second? The second page is creating another session on the file system and not looking to MySQL for it.

          I think I got it.. Looks like it was a problem in the select method. I replace it with this and it works. Also, I have to instantiate the sessions class at the beginning of every script and not with the sess_start() function alone.

          	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["$this->tblvalue"];
          			return $value;
          		} else {
          			return "";
          		}
          	}
            Write a Reply...