Page 1 of 1

session_set_save_hanlder

Posted: Fri Aug 29, 2008 10:35 am
by dude81
Hi,
I've done a code with session_set_save_handler, for handling sessions through database. I implemented all the methods in a session class
Here is the code

Code: Select all

 
Class Session{
    
    public $lifetime;
    
    protected $db;
    
    public $data = array();
    
    public function open($savePath,$sessionName){
 
        $this->lifetime = get_cfg_var("session.gc_maxlifetime");
        $this->db = new DB();
        
        if(!$this->db){
            return false;
        }
        $this->data = $this->read(session_id());
        return true;
            
    }
    
    public function close(){
        
        return true;
        
    }
    
    public function read($sessionID){
        global $our_var;
        
        $user_id = $our_var['user_id'];
        $sql = "SELECT session_data from sessions where session_key ='$sessionID' and user_id = '$user_id' ";
        $res = $this->db->executeSelectArray($sql);
        
        if(count($res) != 0){
            return $res[0];
        }
        return null;
        
    }
    
    public function write($sessionID,$sessionData){
        $newExp = time() + $this->lifetime;
        
        global $our_var;
        $user_id = $our_var['user_id'];
        $sql = "SELECT session_data from sessions where session_key ='$sessionID' and user_id = '$user_id' ";
        $res = $this->db->executeSelect($sql);
        if(count($res)>0){
            $update_query = "UPDATE sessions SET modify_date=NOW(),session_expires='".$newExp."',session_data='".serialize($sessionData)."' WHERE session_key='".$sessionID."' and user_id='".$user_id."' ";
            $count = $this->db->executeQuery($update_query);
            
            if($count >0 ){
                return $this->read($sessionID);
            }
        }else{
            $insert = "INSERT into sessions(user_id,session_key,session_data,begin_time,session_expires,create_date,modify_date) VALUES('$user_id','$sessionID','".serialize($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;
        
        
    }
    
    
} 
 
Unfortunately, when I do session_start() and set an parameter $_SESSION['info'] and access it through read method in the same page, I'm not able to retrieve the value. It works only when I refresh. Can somebody explain, where am I doing mistake here. I'm sorry, I've to edit some variables..

Re: session_set_save_hanlder

Posted: Fri Aug 29, 2008 10:39 am
by Oren
I'm not really sure what/where is the problem, but you might want to look at: viewtopic.php?t=54101

Re: session_set_save_hanlder

Posted: Fri Aug 29, 2008 11:02 am
by dude81
Thank You for the help Oren :), I love to hang on this forum .. but I always failed to spend time. Thank You once again :D :D

Re: session_set_save_hanlder

Posted: Mon Sep 01, 2008 6:08 am
by dude81
Correct me if I was wrong, once we implement, Once we do these methods

Code: Select all

 
session_set_save_handler(array('Session', 'open'),
                         array('Session', 'close'),
                         array('Session', 'read'),
                         array('Session', 'write'),
                         array('Session', 'destroy'),
                         array('Session', 'gc')
                         );
 
 
we can use ordinary sessions functions normally, or should we use these methods to access the session data

Re: session_set_save_hanlder

Posted: Mon Sep 01, 2008 6:18 am
by Oren
Correct, use session/session functions normally :wink:

Re: session_set_save_hanlder

Posted: Mon Sep 01, 2008 10:48 am
by dude81
I wrote a code, which was not working exactly as estimated, all the references were helpful, but most of them seem to be erasing the old session data and replacing the new one. For Example I set

Code: Select all

 
$_SESSION['info'] = "I'm in admin page";
$_SESSION['alpha'] = 10;
 
and in the next page

Code: Select all

 
$_SESSION['info'] = "I'm in searchpage";
 
 
the entire session data is replaced with $_SESSION['info'], and $_SESSION['alpha'] is erased which is the not the purpose of SESSION handling and it seemed impossible to unserialize the data inside the session_set_save_hanlder methods. Probably Feyd's Tutorial in Sticky's seems to have addressed it. Some more references from this angle should be of help

Re: session_set_save_hanlder

Posted: Mon Sep 01, 2008 12:41 pm
by Oren
I really don't know what you are doing there, you sure you know how to work with sessions? are you sure the "session" class you use is written correctly?
Anyway, you might want to check out session_write_close().