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
I have to agree. Is there something specific you are looking for that the normal session management system wouldn't work for?
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.
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:
i am using Zend Session and works fine for me $namespace = new Zend_Session_Namespace( 'namespace' ); $namespace->session = "some value"; [/ode] Code (markup):