Session set save handler problem

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Session set save handler problem

Post by dude81 »

I'm using a user custom session through DB. And I use MVC (built myself) with index.php controlling the routing
controller.php

Code: Select all

 
ini_set('session.save_handler', 'user');
 if (version_compare(phpversion(), '5.0.0', '>=')) {
        register_shutdown_function('session_write_close');
 }
 $OPT_SESSION = new Session();
  
 session_set_save_handler(
                        array(&$OPT_SESSION,"open"),
                        array(&$OPT_SESSION,"close"),
                        array(&$OPT_SESSION,"read"),
                        array(&$OPT_SESSION,"write"),
                        array(&$OPT_SESSION,"destroy"),
                        array(&$OPT_SESSION,"gc")
                        );
                                                
 
 
index.php

Code: Select all

 
 require_once('controller.php');
 
 
 clearstatcache();
 //$role = 1;
session_start();
if( isset($_GET['app']) ){
    $x = $_GET['x']? $_GET['y']:'';
    if(!isset($_GET['x']) || empty($_GET['y'])){
        $y = 'index';
    }else{
        $action = $_GET['y'];
    }
 }
 
$user_id = 0;
if(isset($_POST['login'])){
    $username = isset($_POST['username'])? $_POST['username']:'';
    $password = isset($_POST['password'])? $_POST['password']:'';
    if( !empty($username) && !empty($password)){
        $user = new User($username,$password);
        $authenticated = $user->isAuthenticated();
        
        if(!$authenticated){
            $error = true;
        }
        
    }else{
        $error = true;
    }
}
    
if($authenticated == true){
    $_SESSION['userid'] = $user->user_id;
    $_SESSION['role'] = $user->role_id;
    
}
if($_SESSION['role']> AUTHENTICATED_ROLE){
        
        $isparam = (isset($_POST['mod']))? $_POST['mod'] : '';
        
        if(!empty($isparam)){
            
            header("Location: index.php?x=user&y=home");
            exit;
            
        }   
 }
//$accessObj = new CoreAcl();
 
 if(!empty($x)){
    include_once("modules"."/$x"."/$y".".php");
    $default_content = false;
 }else{
    $default_content = true;
 }
   
 
 
 //$acl = new CoreAcl(2,2);
 //$val = $acl->isValid(1.02);
 
 
 if($default_content) {
 
    require_once(CONTROLLER_DIR."/styles/template/header.php");
    require_once(CONTROLLER_DIR."/styles/template/left.php");
    
    $xtpl = new Xtemplate('styles/index.xtpl');
    $xtpl->parse('main');
    $xtpl->out('main');
    require_once(CONTROLLER_DIR."/styles/template/right.php");
    require_once(CONTROLLER_DIR."/styles/template/footer.php");
 } 
 
 
 
 
And my session file looks like this

Code: Select all

 
Class Session{
    
    public $lifetime;
    
    protected $db;
    
    public $data = array();
    
    function __construct(){
        $this->db = new DB();
        
        if(!$this->db){
            return false;
        }
    }
    public function open($savePath,$sessionName){
 
        $this->lifetime = get_cfg_var("session.gc_maxlifetime");
        //$this->read(session_id()));   
        return true;
    }
    
    public function close(){
        //echo $sessionID;
        return true;
    }
    
    public function read($sessionID){
       
        
        $sql = "SELECT session_data from sessions where session_key ='$sessionID'";
        $res = $this->db->executeSelectArray($sql);
        
        if(count($res) != 0){
            $session_data = $res[0]['session_data'];
            $session = unpackSessionData($session_data);
            return $session;
        }
        return null;
    }
    
    public function write($sessionID,$sessionData){
        $newExp = time() + $this->lifetime;
     
        $sql = "SELECT session_data from sessions where session_key ='$sessionID' ";
        $res = $this->db->executeSelectArray($sql);
        
        
        if(count($res)>0){
            $add = $res[0]['session_data'];
            $update_query = "UPDATE sessions SET modify_date=NOW(),session_expires='".$newExp."',session_data='".$sessionData."' WHERE session_key='".$sessionID."'  ";
            $count = $this->db->executeQuery($update_query);
            
            if($count >0 ){
                return $this->read($sessionID);
            }
        }else{
            $insert = "INSERT into sessions(session_key,session_data,begin_time,session_expires,create_date,modify_date) VALUES('$sessionID','".$sessionData."',NOW(),'$this->lifetime',NOW(),NOW())";
            $count = $this->db->executeQuery($insert);
            if($count >0 ){
                return $this->read($sessionID);
                
            }
            return false;
        }
        
    }
    
    
    public function destroy($sessionID){
        
        
        $query = "DELETE  FROM sessions where session_key='$sessionID'";
        $rows = $this->db->executeQuery($query);
        if($rows >0){
            //$this->data = array();
            return true;
        }
        return false;
        
    }
    
    public function gc($sessionMaxLifeTime){
    
        $query = "DELETE  FROM sessions where session_expires< '".time()."'";
        $rows = $this->db->executeQuery($query);
        
        return $rows;
    }
    
} 
 
.

I'm able to get everything working with my PHP native sessions,and even with my session handler Im able retrieve $_SESSION['role'] in index.php and the problem seems to be in redirecting the page, on doing session_start() and retrieving in user module and home.php, I get $_SESSION blank array, Can somebody throw some light on this
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Session set save handler problem

Post by josh »

Did you check to see if the custom methods are being called when they should? Are they?
Post Reply