SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,048
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)

    Session Management Class And Table Structure

    This is the first class I've written to manage sessions with a database. The class and table structure is based off what I read in this article. If there is a better way to write the implementation for any methods or if something is completely wrong I would appreciate some guidance in correcting it. - thanks

    Table Structure
    Code SQL:
    CREATE TABLE sessions (
     
    	id VARCHAR(100) NOT NULL DEFAULT ''
    	,`data` text NOT NULL
    	,expiration INT(11) UNSIGNED NOT NULL DEFAULT '0'
    	,PRIMARY KEY(id)
     
    ) TYPE=MYISAM;

    Session Management Class
    PHP Code:
    <?php
    class SessionManager {
        
        const 
    table            =        'sessions';
        
        const 
    id             =         'id';
        const 
    data            =         'data';
        const 
    expiration    =        'expiration';

        protected 
    $_life;
        protected 
    $_db;

        public function 
    __construct(PDO $pDb) {
        
            
    $this->_life get_cfg_var('session.gc_maxlifetime');
            
    $this->_db $pDb;
            
            
    $this->_init();
        
        }
        
        protected function 
    _init() {
        
            
    $this->_register();
        
        }
        
        protected function 
    _register() {
        
            
    session_set_save_handler(
                array(
    $this,'open')
                ,array(
    $this,'close')
                ,array(
    $this,'read')
                ,array(
    $this,'write')
                ,array(
    $this,'destroy')
                ,array(
    $this,'gc')
            );
        
        }
        
        public function 
    open($pSavePath,$pSessionName) {
        
            global 
    $sessSavePath;
            
            
    $sessSavePath $pSavePath;
            
            return 
    true;
            
        }
        
        public function 
    close() {
        
            return 
    true;
        
        }
        
        public function 
    read($pId) {
        
            
    $data '';        
            
    $time time();
            
            
    $sql 'SELECT t0.'.self::id.' AS id, t0.'.self::data.' AS data, t0,'.self::expiration.' AS expiration FROM '.self::table.' AS t0 WHERE t0.'.self::id.'= :id AND t0.'.self::expiration.' > :expiration LIMIT 1';
            
            if(
    $stmt $this->_db->prepare($sql)) {
            
                
    $stmt->bindParam(':id',$pId,PDO::PARAM_STR);
                
    $stmt->bindParam(':expiration',$time,PDO::PARAM_INT);
                
                if(
    $stmt->execute()) {
                
                    if(
    $row $stmt->fetch(PDO::FETCH_ASSOC)) {
                        
                        
    $data $row['data'];
                    
                    }
                
                }
            
            }
            
            return 
    $data;
        
        }
        
        public function 
    write($pId,$pData) {
        
            
    $time time() + $this->_life;
            
            
    $sql 'REPLACE '.self::table.' ('.self::id.','.self::data.','.self::expiration.') VALUES (:id,:data,:expiration)';
            
            if(
    $stmt $this->_db->prepare($sql)) {
            
                
    $stmt->bindParam(':id',$pId,PDO::PARAM_STR);
                
    $stmt->bindParam(':data',$pData,PDO::PARAM_STR);
                
    $stmt->bindParam(':expiration',$time,PDO::PARAM_INT);
                
                if(
    $stmt->execute()) {
                
                    return 
    true;
                
                }
            
            }
        
        }
        
        public function 
    destroy($pId) {
        
            
    $sql 'DELETE FROM '.self::table.' WHERE '.self::id.' = :id LIMIT 1';
            
            if(
    $stmt $this->_db->prepare($sql)) {
            
                
    $stmt->bindParam(':id',$pId,PDO::PARAM_STR);
                
                if(
    $stmt->execute()) {
                    
                    return 
    true;
                
                }
            
            }
        
        }
        
        public function 
    gc() {
        
            
    $sql 'DELETE FROM '.self::table.' WHERE '.self::expiration.' < UNIX_TIMESTAMP();';
            
            
    $this->_db->query($sql);
            
            return 
    true;
        
        }
        
        

    }
    ?>

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Still reading Oddz, but I would think the use of global in SessionManager::Open would be a concern for me.

    Why are you using this?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This doesn't prevent concurrent read/write access to a session, if that's something you want. The default files handler does this, but most custom database and memory handlers don't.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •