sjwspud Posted May 10, 2009 Share Posted May 10, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/157539-using-objects-in-session_set_save_handlers/ Share on other sites More sharing options...
MadTechie Posted May 10, 2009 Share Posted May 10, 2009 sounds like $db isn't being set, maybe use mysql_connect to setup the $db in session_open function Quote Link to comment https://forums.phpfreaks.com/topic/157539-using-objects-in-session_set_save_handlers/#findComment-830639 Share on other sites More sharing options...
PFMaBiSmAd Posted May 10, 2009 Share Posted May 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157539-using-objects-in-session_set_save_handlers/#findComment-830651 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.