The examples in this article describe how session is stored in database usage. Share to everyone for your reference. as follows:
<?php/* CREATE TABLE ' ws_sessions ' (' session_id ' varchar (255) binary not NULL default ', ' session_expires ' int (10
Unsigned not NULL default ' 0 ', ' session_data ' text, PRIMARY KEY (' session_id ') Type=innodb;
*/class Session {//session-lifetime var $lifeTime;
Mysql-handle var $dbHandle; function open ($savePath, $sessName) {//get session-lifetime $this->lifetime = Get_cfg_var ("Session.gc_maxlifetime"
);
Open Database-connection $dbHandle = @mysql_connect ("localhost", "root", "");
$dbSel = @mysql_select_db ("Test", $dbHandle);
Return success if (! $dbHandle | |! $dbSel) return false;
$this->dbhandle = $dbHandle;
return true;
function Close () {$this->GC (ini_get (' session.gc_maxlifetime '));
Close Database-connection return @mysql_close ($this->dbhandle); function Read ($sessID) {//fetch session-data $res = mysql_query ("Select Session_data as D from Ws_sessions wher E session_id = ' $sessID ' and Session_expires > '. Time (), $this->dbhandle);
Return data or a empty string at failure if ($row = Mysql_fetch_assoc ($res)) return $row [' d '];
Return "";
function Write ($sessID, $sessData) {//new Session-expire-time $NEWEXP = time () + $this->lifetime;
is a sessions with this ID in the database?
$res = mysql_query ("select * from ws_sessions WHERE session_id = ' $sessID '", $this->dbhandle); If yes, if (mysql_num_rows ($res)) {//... update session-data mysql_query ("Update ws_sessions SET Session_expir
es = ' $newExp ', session_data = ' $sessData ' WHERE session_id = ' $sessID ', $this->dbhandle);
If something happened, return True if (Mysql_affected_rows ($this->dbhandle)) return true;
}//If no session-data was found, else {//Create a new row mysql_query (INSERT into ws_sessions (session_id,
Session_expires, Session_data) VALUES (' $sessID ', ' $newExp ', ' $sessData ') ", $this->dbhandle);
If row is created, return True if (Mysql_affected_rows ($this->dbhandle)) return true;
}//An unknown error occured return false; function Destroy ($sessID) {//delete session-data mysql_query ("Delete from ws_sessions WHERE session_id = ' $sessID '"
, $this->dbhandle);
If session is deleted, return true, or if (Mysql_affected_rows ($this->dbhandle)) return true;
. else return false return false; The function gc ($SESSMAXLIFETIME) {//delete old sessions mysql_query ("Delete from ws_sessions WHERE session_expires ;
". Time (), $this->dbhandle);
Return affected rows return Mysql_affected_rows ($this->dbhandle);
}} $session = new session ();
Session_set_save_handler Array (& $session, "open"), Array (& $session, "Close"), Array (& $session, "read"),
Array (& $session, "write"), Array (& $session, "destroy"), Array (& $session, "GC"));
Session_Start ();
Etc ...
?>
I hope this article will help you with your PHP programming.