PHP writes session to database sample

Source: Internet
Author: User
Tags empty php class sprintf

  This article mainly introduces PHP to write session to the database example, need friends can refer to the following

  Code as follows: <?php class Session_handler {   protected $maxlifetime = null;  protected $dbHandle = null ;  public $config = null;    public static function init ($args) {  return new self ($args); &nbsp}    public function __co NStruct ($args) {    $this->config = $args;   $this->maxlifetime = Get_cfg_var ("session.gc_ Maxlifetime ");   Session_set_save_handler (  array ($this, "open"),   Array ($this, "close"),   Array ($this, "read"),   Array ($this, "write"),   Array ($this, "destroy"),   Array ($this, "GC"));   &NBSP}    public function open () {  $this->link = mysqli_connect (  $this->config[' host '],    $this->config[' user '],    $this->config[' password '],    $this->config[ ' Database ']);   Mysqli_set_charset ($this->link, "UTF8");   $sql = ' CREATE TABLE IF not EXISTS '%s ' (  ' session_id ' varchar (255) NOT null,   ' session_data ' text,   ' Session_expires ' char (TEN) not NULL,   PRIMARY KEY (' session_id ')  ) Engine=innodb DEFAULT Charset=utf8 collate=utf8_unicode_ci; ';   $sql = sprintf ($sql, $this->config[' table ');   Mysqli_query ($this->link, $sql);   return true; &NBSP}    public function close () {  return true; &nbsp}      public function Read ($sessi ON_ID) {  if (empty ($session _id)) return null;     $sql = ' SELECT ' session_data ' as ' data ' '%s ' WHERE ' session_id ' = '%s ' and ' session_expires ' > '%u ';   $sql = sprintf ($sql,    mysqli_real_escape_string ($this->link, $this->config[' table '),    Mysqli_real_escape_string ($this->link, $session _id),   time ());     $result = Mysqli_query ($this->link, $sql);   $row = MYSQLI_FETCH_ASSOC ($result);   return $row [' data '];   &NBSP}    public function write ($session _id, $session_data) {  if (empty ($session _id)) return null;     $newExpires = time () + $this->maxlifetime;   $SQ L = ' REPLACE into '%s ' SET ' session_id ' = '%s ', ' session_data ' = '%s ', ' session_expires ' = '%u ' ';   $sql = sprintf ($sql,    mysqli_real_escape_string ($this->link, $this->config[' table '),    Mysqli_real_escape_string ($this->link, $session _id),   mysqli_real_escape_string ($this->link, $ Session_data),   $newExpires);     $result = Mysqli_query ($this->link, $sql);   Return Mysqli_affected_rows ($this->link);      }    public function destroy ($session _id) {  $sql = ' DELETE from '%s ' WHERE ' Session_ id ' = '%s ';   $sql = sprintf ($sql,    mysqli_real_escape_string ($this->link, $this->config[' table '),    Mysqli_real_escape_string ($this->link, $session _id));     $result = Mysqli_query ($this->link, $sql);   Return MYSQLI_AFFECTed_rows ($this->link);    }    public function gc () {  $sql = ' DELETE from '%s ' WHERE ' session_expires ' < '%u '; &n Bsp $sql = sprintf ($sql,    mysqli_real_escape_string ($this->link, $this->config[' table ')],    Time ());     $result = Mysqli_query ($this->link, $sql);   Return Mysqli_affected_rows ($this->link);    }  }   class Session {   public static $collection = null;    public static function Open ($clean = False, $token = False) {  if ($clean) Ob_end_clean ()   if ($token) session_id ($token); &n Bsp Session_Start ();   Self:: $collection = $_session; &NBSP}    public static function id () {  $num _args = Func_num_args ();   if ($num _args) {  &NBSP ; $args = func_get_arg (0);    return session_id ($args);  }else{   return session_id ();  }      public static function Get ($name) {&nbsP return Isset ($_session[$name])? $_session[$name]: null; &NBSP}    public static function set ($name, $value) {  $_session[$name] = $value;   return true; &nb SP;}    public static function Delete ($name) {  if (!isset ($_session[$name])) return null;   unset ($_sessi on[$name]);   return true; &NBSP}    public static function Destroy () {  Session_destroy ();  }  }   $config = Array ( "Host" => "127.0.0.1", "User" => "root", "password" => "123456", "Database" => "test", "CharSet" => "UTF8", "t Able "=>" user_session ");   Session_handler::init ($config); Session::open (); Session::set ("Profile", Array ("id" => 1, "User" => "Haowei", "Vip-level" => 6));
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.