Session stored in database usage examples _php tips

Source: Internet
Author: User
Tags php programming sessions

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-&GT;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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.