Hi guys, I'm not new to PHP, but I'm new to OOP in PHP. I can't figure out why the following wont work. If I break the methods down outside the class into functions it works, but here it doesn't. The session_set method is suppose to interact with the other methods but I dont think it is and I'm not sure what to do. Any help is appreciated!!!
/*
* Session handling class
* Instantiate with db host, user, pass, db, table, field sess id, field expiration, field value
*/
class Sessions {
var $sqlhost;
var $sqluser;
var $sqlpass;
var $sqldb;
var $sqltable;
var $tblsid;
var $tblexpire;
var $tblvalue;
/*
* Construct the class
*/
function Sessions($sqlhost, $sqluser, $sqlpass, $sqldb, $sqltable, $tblsid, $tblexpire, $tblvalue) {
$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 value FROM sessioninfo
WHERE sid = '$sid' AND
expiration > ". 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) {
$lifetime = get_cfg_var("session.gc_maxlifetime");
$expiration = time() + $lifetime;
$sql = "INSERT INTO sessioninfo
VALUES('$sid', '$expiration', '$value')";
$result = mysql_query($sql);
if (! $result) {
$sql = "UPDATE sessioninfo SET
expiration = '$expiration',
value = '$value' WHERE
sid = '$sid' AND expiration >". 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 sessioninfo
WHERE sid = '$sid'";
$result = mysql_query($sql);
}
/*
* mysql_session_garbage_collect()
* Deletes all sessions that have expired.
*/
function mysql_session_garbage_collect($lifetime) {
$sql = "DELETE FROM sessioninfo
WHERE sess_expiration < ".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("mysql_session_open",
"mysql_session_close",
"mysql_session_select",
"mysql_session_write",
"mysql_session_destroy",
"mysql_session_garbage_collect");
}
}
I call it like this:
$newsess = new Sessions(SQLHOST, SQLUSER, SQLPASS, SQLDB, sessioninfo, sid, expiration, value);
$newsess->session_set();
session_start();
$_SESSION['name'] = "Test Name";
All the data being passed to Sessions is valid.
I get the following error: Fatal error: session_start(): Failed to initialize storage module.
Thanks in advance for your help!!