php 5.0.3 , apache 2, fedora 3
i am building a custom session handler and am having issue with it in PHP5
for some reason the methods are not accesing the $_phpSessionID of the $_nativeSessionID
if i do a var_dump of $this from inside the and methods of the object those 2 come back as NULL .... unles i do the var_dump from _sessionReadMethod() .... then i get all of the back filled. i don't get it .. isn't the _sessionReadMethod() suipposed to complete before and other methods can run? and if it is why does it see them as filled and none of the other methods do .... any help would be greatly apprecieted ... here is the code :
<?php
class UserSession{
private $_dataObj;
private $_phpSessionID;
private $_nativeSessionID;
private $_loggedIn;
private $_userID;
private $_sessionTimeOut = 30;
private $_sessionLifeSpan = 30;
public function __construct(){
if(class_exists('DBAL')){
$this->_dataObj = new DBAL();
}elseif(class_exists('ErrorReport')){
$error = 1;
}else{
die('Session DataBase Connection Failed. DBAL Class could not be found.');
}
session_set_save_handler(
array(&$this, '_sessionOpenMethod'),
array(&$this, '_sessionCloseMethod'),
array(&$this, '_sessionReadMethod'),
array(&$this, '_sessionWriteMethod'),
array(&$this, '_sessionDestroyMethod'),
array(&$this, '_sessionGcMethod')
);
$strUserAgent = $_SERVER['HTTP_USER_AGENT'];
if(isset($_COOKIE['PHPSESSID']) && !empty($_COOKIE['PHPSESSID'])){
$this->_phpSessionID = $_COOKIE['PHPSESSID'];
$qString = "SELECT user_sess_id FROM `user_session` WHERE ascii_session_id='".$this->_phpSessionID."' AND ((".time()." - created) < ".$this->_sessionLifeSpan.") AND user_agent='".$strUserAgent."' AND ((".time()." - last_impression) <= ".$this->_sessionTimeOut." OR last_impression IS NULL)";
$results = $this->_dataObj->dbSelect($qString);
$numRows = $this->_dataObj->dbTotalRows();
if($numRows == 0){
$fail = 1;
$results = $this->_dataObj->dbDelete('user_session', "ascii_session_id='". $this->_phpSessionID."' OR (".time()." - created) > ".$this->_sessionLifeSpan);
$qString = "SELECT user_sess_id FROM user_session";
$results = $this->_dataObj->dbSelect($qString);
$rowCount = $this->_dataObj->dbTotalRows();
if($rowCount > 0){
$i = 0;
while($i < $rowCount){
$rows = $results[$i];
$checkString = $rows['user_sess_id'].",";
$i++;
}
$cStringCount = strlen($checkString);
$newCStringLength = $qStringCount - 1;
$newCString = substr($checkString, 0, $newCStringLength);
$results = $this->_dataObj->dbDelete('session_variable', "session_id NOT IN (".$newCString.")");
}else{
$results = $this->_dataObj->dbDelete('session_variable');
}
unset($_COOKIE["PHPSESSID"]);
}
}
session_set_cookie_params($this->_sessionLifeSpan);
session_start();
}
public function impress(){
if ($this->_nativeSessionID){
$updateArray = array(
'last_impression'=>time()
);
$results = $this->_dataObj->dbUpdate($updateArray,'user_session',"user_sess_id=".$this->_nativeSessionID);
}
}
public function isLoggedIn(){
return $this->_loggedIn;
}
public function getUserID(){
if($this->_loggedIn){
return $this->_userID;
}else{
return false;
}
}
public function getUserObject(){
if($this->_loggedIn){
if (class_exists("User")){
$objUser = new User($this->_userID);
return $objUser;
}else{
return false;
}
}
}
public function getSessionIdentifier(){
return $this->_phpSessionID;
}
public function logIn($strUsername, $strPlainPassword){
$strMD5Password = md5($strPlainPassword);
$qString = "SELECT `user_id` FROM `user` WHERE `user_name`='".$strUsername."' AND `encrypt_passwd`= '".$strMD5Password."'";
$results = $this->_dataObj->dbSelect($qString);
$totalRows = $this->_dataObj->dbTotalRows();
if($totalRows > 0){
$row = $results[0];
$this->_userID = $row['user_id'];
$this->_loggedIn = true;
$updateArray = array(
'logged_in'=>true,
'user_id'=>$this->_userID
);
$results = $this->_dataObj->dbUpdate($updateArray,'user_session',"user_sess_id='".$this->_nativeSessionID."'");
if($this->_dataObj->dbRowsAffected() <> 1){
$this->_loggedIn = false;
$this->_userID = 0;
return false;
}else{
$this->_loggedIn = true;
return true;
}
}else{
return false;
}
}
public function logOut(){
if ($this->_loggedIn == true){
$updateArray = array(
'logged_in'=>false,
'user_id'=>0
);
$results = $this->_dataObj->dbUpdate($updateArray,'user_session',"user_sess_id='".$this->_nativeSessionID."'");
if($this->_dataObj->dbRowsAffected() <> 1){
$this->_loggedIn = true;
return false;
}else{
$this->_loggedIn = false;
$this->_userID = 0;
return true;
}
}else{
return false;
}
}
public function __get($nm){
$results = $this->_dataObj->dbSelect("SELECT `variable_value` FROM `session_variable` WHERE `session_id`='".$this->_nativeSessionID."' AND `variable_name`='" .$nm."'");
$totalRows = $this->_dataObj->dbTotalRows();
if($totalRows>0){
$row = $result[0];
return unserialize($row['variable_value']);
}else{
return false;
}
}
public function __set($nm, $val){
$strSer = serialize($val);
$insertArray = array(
'session_id'=>$this->_nativeSessionID,
'variable_name'=>$nm,
'variable_value'=>$strSer
);
$results = $this->_dataObj->dbInsert($insertArray,'session_variable');
$this->_ranFirst = "TRUE";
}
public function _sessionOpenMethod($savePath, $sessionName){
return true;
}
public function _sessionCloseMethod(){
$this->_dataObj = null;
return true;
}
private function _sessionReadMethod($id){
$strUserAgent = $_SERVER["HTTP_USER_AGENT"];
$this->_phpSessionID = $id;
$failed = 1;
$results = $this->_dataObj->dbSelect("SELECT user_sess_id, logged_in, user_id FROM `user_session` WHERE ascii_session_id = '".$id."'");
$totalRows = $this->_dataObj->dbTotalRows();
if($totalRows > 0){
$row = $results[0];
$this->_nativeSessionID = $row['user_sess_id'];
if($row['logged_in'] == "t"){
$this->_loggedIn = true;
$this->_userID = $row['user_id'];
}else{
$this->_loggedIn = false;
}
}else{
$this->_loggedIn = false;
$insertArray = array(
'ascii_session_id'=>$id,
'logged_in'=>'f',
'user_id'=>0,
'created'=>time(),
'user_agent'=>$strUserAgent
);
$results = $this->_dataObj->dbInsert($insertArray,'user_session');
# Now get the true ID
$this->_nativeSessionID = $this->_dataObj->dbLastID();
}
return "";
}
public function _sessionWriteMethod($id, $sessData){
return true;
}
public function _sessionDestroyMethod($id){
$results = $this->_dataObj->dbDelete('user_session',"ascii_session_id='".$id."'");
return $result;
}
public function _sessionGcMethod($maxLifeTime){
return true;
}
}
?>
i am building a custom session handler and am having issue with it in PHP5
for some reason the methods are not accesing the $_phpSessionID of the $_nativeSessionID
if i do a var_dump of $this from inside the and methods of the object those 2 come back as NULL .... unles i do the var_dump from _sessionReadMethod() .... then i get all of the back filled. i don't get it .. isn't the _sessionReadMethod() suipposed to complete before and other methods can run? and if it is why does it see them as filled and none of the other methods do .... any help would be greatly apprecieted ... here is the code :
<?php
class UserSession{
private $_dataObj;
private $_phpSessionID;
private $_nativeSessionID;
private $_loggedIn;
private $_userID;
private $_sessionTimeOut = 30;
private $_sessionLifeSpan = 30;
public function __construct(){
if(class_exists('DBAL')){
$this->_dataObj = new DBAL();
}elseif(class_exists('ErrorReport')){
$error = 1;
}else{
die('Session DataBase Connection Failed. DBAL Class could not be found.');
}
session_set_save_handler(
array(&$this, '_sessionOpenMethod'),
array(&$this, '_sessionCloseMethod'),
array(&$this, '_sessionReadMethod'),
array(&$this, '_sessionWriteMethod'),
array(&$this, '_sessionDestroyMethod'),
array(&$this, '_sessionGcMethod')
);
$strUserAgent = $_SERVER['HTTP_USER_AGENT'];
if(isset($_COOKIE['PHPSESSID']) && !empty($_COOKIE['PHPSESSID'])){
$this->_phpSessionID = $_COOKIE['PHPSESSID'];
$qString = "SELECT user_sess_id FROM `user_session` WHERE ascii_session_id='".$this->_phpSessionID."' AND ((".time()." - created) < ".$this->_sessionLifeSpan.") AND user_agent='".$strUserAgent."' AND ((".time()." - last_impression) <= ".$this->_sessionTimeOut." OR last_impression IS NULL)";
$results = $this->_dataObj->dbSelect($qString);
$numRows = $this->_dataObj->dbTotalRows();
if($numRows == 0){
$fail = 1;
$results = $this->_dataObj->dbDelete('user_session', "ascii_session_id='". $this->_phpSessionID."' OR (".time()." - created) > ".$this->_sessionLifeSpan);
$qString = "SELECT user_sess_id FROM user_session";
$results = $this->_dataObj->dbSelect($qString);
$rowCount = $this->_dataObj->dbTotalRows();
if($rowCount > 0){
$i = 0;
while($i < $rowCount){
$rows = $results[$i];
$checkString = $rows['user_sess_id'].",";
$i++;
}
$cStringCount = strlen($checkString);
$newCStringLength = $qStringCount - 1;
$newCString = substr($checkString, 0, $newCStringLength);
$results = $this->_dataObj->dbDelete('session_variable', "session_id NOT IN (".$newCString.")");
}else{
$results = $this->_dataObj->dbDelete('session_variable');
}
unset($_COOKIE["PHPSESSID"]);
}
}
session_set_cookie_params($this->_sessionLifeSpan);
session_start();
}
public function impress(){
if ($this->_nativeSessionID){
$updateArray = array(
'last_impression'=>time()
);
$results = $this->_dataObj->dbUpdate($updateArray,'user_session',"user_sess_id=".$this->_nativeSessionID);
}
}
public function isLoggedIn(){
return $this->_loggedIn;
}
public function getUserID(){
if($this->_loggedIn){
return $this->_userID;
}else{
return false;
}
}
public function getUserObject(){
if($this->_loggedIn){
if (class_exists("User")){
$objUser = new User($this->_userID);
return $objUser;
}else{
return false;
}
}
}
public function getSessionIdentifier(){
return $this->_phpSessionID;
}
public function logIn($strUsername, $strPlainPassword){
$strMD5Password = md5($strPlainPassword);
$qString = "SELECT `user_id` FROM `user` WHERE `user_name`='".$strUsername."' AND `encrypt_passwd`= '".$strMD5Password."'";
$results = $this->_dataObj->dbSelect($qString);
$totalRows = $this->_dataObj->dbTotalRows();
if($totalRows > 0){
$row = $results[0];
$this->_userID = $row['user_id'];
$this->_loggedIn = true;
$updateArray = array(
'logged_in'=>true,
'user_id'=>$this->_userID
);
$results = $this->_dataObj->dbUpdate($updateArray,'user_session',"user_sess_id='".$this->_nativeSessionID."'");
if($this->_dataObj->dbRowsAffected() <> 1){
$this->_loggedIn = false;
$this->_userID = 0;
return false;
}else{
$this->_loggedIn = true;
return true;
}
}else{
return false;
}
}
public function logOut(){
if ($this->_loggedIn == true){
$updateArray = array(
'logged_in'=>false,
'user_id'=>0
);
$results = $this->_dataObj->dbUpdate($updateArray,'user_session',"user_sess_id='".$this->_nativeSessionID."'");
if($this->_dataObj->dbRowsAffected() <> 1){
$this->_loggedIn = true;
return false;
}else{
$this->_loggedIn = false;
$this->_userID = 0;
return true;
}
}else{
return false;
}
}
public function __get($nm){
$results = $this->_dataObj->dbSelect("SELECT `variable_value` FROM `session_variable` WHERE `session_id`='".$this->_nativeSessionID."' AND `variable_name`='" .$nm."'");
$totalRows = $this->_dataObj->dbTotalRows();
if($totalRows>0){
$row = $result[0];
return unserialize($row['variable_value']);
}else{
return false;
}
}
public function __set($nm, $val){
$strSer = serialize($val);
$insertArray = array(
'session_id'=>$this->_nativeSessionID,
'variable_name'=>$nm,
'variable_value'=>$strSer
);
$results = $this->_dataObj->dbInsert($insertArray,'session_variable');
$this->_ranFirst = "TRUE";
}
public function _sessionOpenMethod($savePath, $sessionName){
return true;
}
public function _sessionCloseMethod(){
$this->_dataObj = null;
return true;
}
private function _sessionReadMethod($id){
$strUserAgent = $_SERVER["HTTP_USER_AGENT"];
$this->_phpSessionID = $id;
$failed = 1;
$results = $this->_dataObj->dbSelect("SELECT user_sess_id, logged_in, user_id FROM `user_session` WHERE ascii_session_id = '".$id."'");
$totalRows = $this->_dataObj->dbTotalRows();
if($totalRows > 0){
$row = $results[0];
$this->_nativeSessionID = $row['user_sess_id'];
if($row['logged_in'] == "t"){
$this->_loggedIn = true;
$this->_userID = $row['user_id'];
}else{
$this->_loggedIn = false;
}
}else{
$this->_loggedIn = false;
$insertArray = array(
'ascii_session_id'=>$id,
'logged_in'=>'f',
'user_id'=>0,
'created'=>time(),
'user_agent'=>$strUserAgent
);
$results = $this->_dataObj->dbInsert($insertArray,'user_session');
# Now get the true ID
$this->_nativeSessionID = $this->_dataObj->dbLastID();
}
return "";
}
public function _sessionWriteMethod($id, $sessData){
return true;
}
public function _sessionDestroyMethod($id){
$results = $this->_dataObj->dbDelete('user_session',"ascii_session_id='".$id."'");
return $result;
}
public function _sessionGcMethod($maxLifeTime){
return true;
}
}
?>