What's the Best Session Management Class?

Discussion in 'PHP' started by mrphp, Jul 22, 2009.

  1. #1
    Hello,

    Can anyone tell me the best Session Management Class that we can use for our PHP Applications?

    The class should be latest and uses PHP 5 or later.

    I think a lot of programmers will be benefited with this thread, just a guess.

    Any help is highly appreciated.

    Regards
     
    mrphp, Jul 22, 2009 IP
  2. !Unreal

    !Unreal Well-Known Member

    Messages:
    1,671
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    165
    #2
    Ive always made my own. Sessions are not so advanced they require a class.
     
    !Unreal, Jul 22, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    I have to agree. Is there something specific you are looking for that the normal session management system wouldn't work for?
     
    jestep, Jul 22, 2009 IP
  4. yohanip

    yohanip Well-Known Member

    Messages:
    350
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #4
    the session management class, did you mean the garbage collections?
     
    yohanip, Jul 22, 2009 IP
  5. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sessions are no more difficult than cookies, they expire all the same too, just a matter of where they're stored. Simply put you start a session... and you populate session values, and when you have a log off button you just destroy the session, simple as that.
     
    kblessinggr, Jul 22, 2009 IP
  6. Martinoes

    Martinoes Peon

    Messages:
    110
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Check this links:
    http://www.nateklaiber.com/blog/2006/05/10/custom-php-session-handler
    http://www.tonymarston.net/php-mysql/session-handler.html
    http://www.sccode.com/projects/filemanager/src/session_handler.php

    This is part of code that i use (BaseDao is my database manager class)

    
    <?php
    class SessionHandler extends BaseDao {
    	
    	public function SessionHandler() {
    		parent::BaseDao(new Connection());
    	}
    	
    	public function init() {
    		session_module_name('user');
    		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($sess_path, $sess_name, $persist = null) {
    		return true;
    	}
    	
    	public function close() {
    		return true;
    	}
    	
    	public function read($sess_id) {
    		$querySelect = "SELECT session_data FROM tb_session WHERE session_id = :session_id";
    		
    		$result = $this->execute($this->connection->prepareQuery($querySelect, Array("session_id"=>$sess_id)));
    
    		if ($result != null && isset($result[0]) && isset($result[0]["session_data"])) {
    			return $result[0]["session_data"];
    		} else {
    			return '';
    		}
    	}
    	
    	public function write($sess_id, $data) {
    		$sessionHandler = new SessionHelper();
    		$CurrentTime = date('Y-m-d H:i:s');
    		
    		$querySelect = "SELECT session_data FROM tb_session WHERE session_id = :session_id";
    		$queryInsert = "";
    		$queryUpdate = "";
    		
    		$table = Array();
    		
    		if (isset($_SESSION["user"]) && $sessionHandler->isUserLoggedIn($_SESSION["user"])) {
    			$table["username"] = $sessionHandler->getUsernameFromSession($_SESSION["user"]);
    			$queryUpdate = "UPDATE tb_session SET last_updated = :last_updated, session_data = :session_data, username = :username WHERE session_id = :session_id";
    			$queryInsert = "INSERT INTO tb_session (session_id, date_created, last_updated, session_data, username) values (:session_id, :date_created, :last_updated, :session_data, :username) ";
    		} else {
    			$queryUpdate = "UPDATE tb_session SET last_updated = :last_updated, session_data = :session_data WHERE session_id = :session_id";
    			$queryInsert = "INSERT INTO tb_session (session_id, date_created, last_updated, session_data) values (:session_id, :date_created, :last_updated, :session_data) ";
    		}
    		
    		$table["last_updated"] = $CurrentTime;
    		$table["date_created"] = $CurrentTime;
    		$table["session_id"] = $sess_id;
    		$table["session_data"] = $data;
    		
    		$result = $this->execute($this->connection->prepareQuery($querySelect, $table));
    		
    		if ($result != null && count($result) > 0) {
    			$this->update($this->connection->prepareQuery($queryUpdate, $table));
    		} else {
    			$this->insert($this->connection->prepareQuery($queryInsert, $table));
    		}
    		
    		return true;
    	}
    	
    	public function destroy($sess_id) {
    		$query = "DELETE FROM tb_session WHERE session_id = :session_id";
    		
    		$this->delete($this->connection->prepareQuery($query, Array("session_id" => $sess_id)));
    		
    		return true;
    	}
    	
    	public function gc($max_lifetime) {
            $real_now = date('Y-m-d H:i:s');
            $dt1 = strtotime("$real_now -$max_lifetime seconds");
            $dt2 = date('Y-m-d H:i:s', $dt1);
            
            $query = "DELETE FROM tb_session WHERE last_updated < :last_updated";
    		
            $this->delete($this->connection->prepareQuery($query, Array("last_updated" => $dt2)));
            
            return true;
        } 
    }
    ?>
    
    PHP:
     
    Martinoes, Jul 22, 2009 IP
  7. pipisdicelana

    pipisdicelana Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i am using Zend Session and works fine for me

    
    $namespace = new Zend_Session_Namespace( 'namespace' );
    $namespace->session = "some value";
    [/ode]
    Code (markup):
     
    pipisdicelana, Jul 22, 2009 IP