Jump to content

Using objects in session_set_save_handlers();


sjwspud

Recommended Posts

I want to save all sessions into a database instead of the default files, etc.

 

I have created functions for each callback, open, read, etc. My function looks like this:

 

session_set_save_handler("session_open", "session_end", "session_read", "session_write", "session_trash", "session_gc");

 

And below are my functions:

 

function session_open($savePath, $sessionName){

error_log($sessionName . " ". session_id());
return (true);

}

function session_end(){

return '';

}

function session_read($id){

error_log($id);

global $db;

$rs = $db->upb_db_query("SELECT * FROM ".SQL_PREFIX."sessions
                             WHERE ses_id = '$id'");

if ($rs){
	$rc = $db->upb_db_fetch_object($rs);
	return $rc;
}  else {
	return false;
}

}

function session_write($id,$val){

global $db;

$val = addslashes($val);

$rs = $db->upb_db_query("SELECT COUNT(*) AS exist FROM ".SQL_PREFIX."sessions
                             WHERE ses_id = '$id'");
$rc = $db->upb_db_fetch_object($rs);

if ($rc->exist > 0){
	$db->upb_db_query("UPDATE ".SQL_PREFIX."sessions
                           SET
  	                       ses_data = '$val',
                           ses_expire = unix_timestamp(date_add(now(), interval 1 hour))
                           WHERE ses_id = '$id'");
	return true;
}  else {
	$db->upb_db_query("INSERT INTO ".SQL_PREFIX."sessions
                           VALUES (
                           '$id',
                           '$val',
                           unix_timestamp(date_add(now(), interval 1 hour))
                           )");
	return true;
}

return false;

}

function session_trash($id){

global $db;

$db->upb_db_query("DELETE FROM ".SQL_PREFIX."sessions
                       WHERE ses_id = '$key'");

return true;

}

function session_gc($max_lifetime){

$db->upb_db_query("DELETE FROM ".SQL_PREFIX."sessions
                       WHERE unix_timestamp(ses_expire) < unix_timestamp(now())");

$rows = $db->upb_db_affected_rows();

return $rows;

}

 

the object $db is declared on my index.php file and is created before I run the handler. For some reason I get the following error:

 

Fatal error: Call to a member function upb_db_query() on a non-object in /home/upb/public_html/betaboard/runtime/session_storage.php on line 68

 

How can this be fixed? Do objects get destroyed before write happens? The problem is occuring during the write function.

 

Any help would be appreciated. Thank you!

From the session_set_save_handler page in the php manual -

Warning

As of PHP 5.0.5 the write and close handlers are called after object destruction and therefore cannot use objects or throw exceptions. The object destructors can however use sessions.

 

It is possible to call session_write_close() from the destructor to solve this chicken and egg problem.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.