session - why would session_set_save_handler not be working PHP 5.2
Here's the code, I'm making an educated guess that sess_read
gets called when a session var is assigned. It is assigning the value. Is there a way I can test this?
function sess_open($sess_path, $sess_name) {
return true;
}
function sess_close() {
return true;
}
function sess_read($sess_id) {
echo "TEST";
$result = mysql_query("SELECT Data FROM sessions WHERE SessionID = '$sess_id';");
if (!mysql_num_rows($result)) {
$CurrentTime = time();
mysql_query("INSERT INTO sessions (SessionID, lastseen) VALUES ('$sess_id', $CurrentTime);") or die(mysql_error());
return '';
} else {
extract(mysql_fetch_array($result), EXTR_PREFIX_ALL, 'sess');
mysql_query("UPDATE sessions SET lastseen = $CurrentTime WHERE SessionID = '$sess_id';");
return $sess_Data;
}
}
function sess_write($sess_id, $data) {
$CurrentTime = time();
mysql_query("UPDATE sessions SET Data = '$data', lastseen = $CurrentTime WHERE SessionID = '$sess_id';");
return true;
}
function sess_destroy($sess_id) {
mysql_query("DELETE FROM sessions WHERE SessionID = '$sess_id';");
return true;
}
function sess_gc($sess_maxlifetime) {
$CurrentTime = time();
mysql_query("DELETE FROM sessions WHERE lastseen + $sess_maxlifetime < $CurrentTime;");
return true;
}
session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
session_start();
ini_set('session.gc-maxlifetime', 60);
$_SESSION['foo'] = "bar";
$_SESSION['baz'] = "wombat";
echo $_SESSION['baz'];
//^^^^This returns wombat but why isn't thesess_read
being called?
EDIT
OK solved it by first of all checkingsession_get_save_handler
was working using ordie()
Then setsession_destroy()
before calling it - this solved the problem. The session had already started in an include somewhere else
Answer
Solution:
if you take a look at your sess_read function you can see that it doesn't return the value for a certain key in the $_SESSION array that you are asking for, it reads the entire session content.
the read function is called one time when your script starts and write when the script ends
between these two calls $_SESSION is similar to a regular array that stores data with the exception that it is a superglobal (you can access it from any scope)