PHP Session Write database, phpsession_php tutorial

Source: Internet
Author: User
Tags php class php session sprintf

PHP Session write database, phpsession


This article introduces the PHP session writing database method, share to everyone for reference, the specific content as follows

<?php # Codemaker Alpha 0.1.1 (haowei.me) # This framework comply with the GPL license agreement# class Session_han  dler {protected $maxlifetime = null;  protected $dbHandle = null;     public $config = null;  public static function init ($args) {return new self ($args);    The Public Function __construct ($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, "WRI       TE "), Array ($this," destroy "), Array ($this," GC ")); 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) is not NULL, ' session_data ' text, ' Session_expir Es ' 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;  Public function Close () {return true;         Public function read ($session _id) {if (Empty ($session _id)) return null;    $sql = ' SELECT ' session_data ' as ' data ' from '%s ' WHERE ' session_id ' = '%s ' and ' session_expires ' > '%u '; $sql = sprintf ($sql, mysqli_real_escape_string ($this->link, $this->config[' table '), Mysqli_real_escape_stri         Ng ($this->link, $session _id), Time ());    $result = Mysqli_query ($this->link, $sql);    $row = Mysqli_fetch_assoc ($result);       return $row [' data '];         Public Function Write ($session _id, $session _data) {if (Empty ($session _id)) return null;    $newExpires = time () + $this->maxlifetime; $sql = ' 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_stri         Ng ($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_stri     Ng ($this->link, $session _id));    $result = Mysqli_query ($this->link, $sql);  Return Mysqli_affected_rows ($this->link);    The Public Function gc () {$sql = ' DELETE from '%s ' WHERE ' session_expires ' < '%u ';         $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);    Session_Start ();  Self:: $collection = $_session;    public static function ID () {$num _args = Func_num_args ();      if ($num _args) {$args = Func_get_arg (0);    return session_id ($args);    }else{return session_id ();  }} public static function get ($name) {return isset ($_session[$name]) $_session[$name]: null;    public static function set ($name, $value) {$_session[$name] = $value;  return true;    public static function Delete ($name) {if (!isset ($_session[$name])) return null;    Unset ($_session[$name]);  return true;  } public static function Destroy () {Session_destroy (); }} $config = Array ("host" = "127.0.0.1", "user" = "root", "password" = "123456", "database" = "test", "Charse T "=" UTF8 "," table "=> "User_session"); Session_handler::init ($config); Session::open (); Session::set ("Profile", Array ("id" = 1, "User" = "Haowei", " Vip-level "= 6));

The above is the whole content of this article, I hope that everyone's study has helped.

Articles you may be interested in:

    • PHP uses MySQL database to store session code
    • Session Save to database PHP class share
    • PHP writes session to database example
    • PHP Standalone Session database storage operation class sharing
    • Use the Session_set_save_handler () function in PHP to save the session to a MySQL DB instance
    • PHP stores session information to a class instance of a database
    • PHP implementation writes session to database
    • PHP class instances for database interaction based on session

http://www.bkjia.com/PHPjc/1099073.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099073.html techarticle PHP session to write to the database, phpsession this article introduces the PHP session to write the database method, share to everyone for reference, specific content as follows PHP # # Codemaker Alpha 0.1 ....

  • Related Article

    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.