Some things are best left OOP
less I guess.
Code: Select all
<?php
class Session
{
private $UserID;
public function __construct($UserID)
{
session_module_name("user");
session_set_save_handler(array($this, "open"),
array($this, "close"),
array($this, "read"),
array($this, "write"),
array($this, "remove"),
array($this, "gc")
);
session_start();
$this->UserID = $UserID;
}
public function open($path, $name)
{
return TRUE;
}
public function close()
{
return TRUE;
}
public function read($id)
{
$Row = mysql_query("SELECT `Data` ... ");
return $Row['Data'];
}
public function write($id, $data)
{
$UserID = $this->UserID; // $UserID seems to be 0 when $_SESSIION['var'] = value; is done
mysql_query("INSERT $id,NOW(),$UserID,$data ... ON DUPLICATE KEY UPDATE ... $data"); // echo $sql
return TRUE;
}
public function remove($id)
{
mysql_query("DELETE ... ");
}
public function gc()
{
return TRUE;
}
}
?>
Code: Select all
$UserID = 1;
$sess = new Session($UserID);
$_SESSION['test_var'] = 'xxx';
Even though this worked, but this statement
$_SESSION['test_var'] = 'xxx'; worked only because of
SessionID being primary in the
database and
ON DUPLICATE KEY UPDATE `Data` did the job.
When I inserted an echo in the session write method, I found that $UserID got a value of 0, when it shouldnt be
mysql_query("INSERT $id,NOW(),$UserID,$data ... ON DUPLICATE KEY UPDATE ... $data"); was
Code: Select all
INSERT INTO `sessions` VALUES ('f1a040midjfer4ga92tb6992f1', NOW(), '0', 'test_var|s:3:\"xxx\";') ON DUPLICATE KEY UPDATE `Data` = 'test_var|s:3:\"xxx\";'
I guess it doesnt make sense creating another instance of
Session class except for the constructor call which passes the right $UserID value to the first write() call.
And how is this treated ?
Code: Select all
session_set_save_handler(array($this, "open"),
array($this, "close"),
array($this, "read"),
array($this, "write"),
array($this, "remove"),
array($this, "gc")
);
Or should I make these functions static ? The only issue with this is the class member data that may be required when reading/writing sessions.