Hi.
Sorry to disturb you again but
it's the first time I manage with
session_set_save_handler therefore
I'd like to know if this snippet is all right.
<?php
require_once('db.php');
require_once('config.php');
// define 'openSession()' function
function openSession($sessionPath,$sessionName){
return true;
}
// define 'closeSession()' function
function closeSession(){
return true;
}
// define 'readSession()' method
function readSession($sessionId){
global $db;
// escape session ID
$sessionId=$db->realEscapeString($sessionId);
$result=$db->query("SELECT sessiondata FROM sessions WHERE sessionid='$sessionId' AND expiry > NOW()");
if($result->countRows()>0){
$row=$result->fetchRow();
return $row->sessiondata;
}
// return empty string
return "";
}
// define 'writeSession()' function
function writeSession($sessionId,$sessionData){
global $db;
$expiry = get_cfg_var('session.gc_maxlifetime')-1;
// escape session ID
$sessionId=$db->realEscapeString($sessionId);
$result=$db->query("SELECT sessionid FROM sessions WHERE sessionid='$sessionId'");
// check if a new session must be stored or an existing one must be updated
($result->countRows()>0)?$db->query("UPDATE sessions SET sessionid='$sessionId',expiry=NOW()+INTERVAL $expiry SECOND,sessiondata='$sessionData' WHERE sessionid='$sessionId'"):$db->query("INSERT INTO sessions (sessionid,expiry,sessiondata) VALUES ('$sessionId',NOW()+INTERVAL $expiry SECOND,'$sessionData')");
return true;
}
// define 'destroySession()' function
function destroySession($sessionId){
global $db;
// escape session ID
$sessionId=$db->realEscapeString($sessionId);
$db->query("DELETE FROM sessions WHERE sessionid='$sessionId'");
return true;
}
// define 'gcSession()' function
function gcSession($maxlifetime){
global $db;
$db->query("DELETE FROM sessions WHERE expiry < NOW()");
return true;
}
session_set_save_handler('openSession','closeSession','readSession','writeSession','destroySession','gcSession');
session_start();
class A
{
public $string;
public function __construct($string)
{
$this->string = $string;
}
}
$a = new A('Whisher');
// register some session variables
$_SESSION['username']=$a;
var_dump($_SESSION['username']->string);
?>
and could I get into trouble managing object in this way ?
Thanks in advance.
Bye.