PHP writes session to database sample _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
Class Session_handler {

protected $maxlifetime = null;
protected $dbHandle = null;
public $config = null;

public static function init ($args) {
return new self ($args);
}

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, "write"),
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_expires ' char (TEN) is 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_string ($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_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 ';
$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) Sessi ON_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",
"CharSet" = "UTF8",
"Table" = "user_session");

Session_handler::init ($config);
Session::open ();
Session::set ("Profile", Array ("id" = + 1, "user" = "Haowei", "vip-level" = 6));

http://www.bkjia.com/PHPjc/736853.html www.bkjia.com true http://www.bkjia.com/PHPjc/736853.html techarticle Copy the code as follows: PHP class Session_handler {protected $maxlifetime = null; protected $dbHandle = null; public $config = Nu ll public static function init ($args) {retur ...

  • 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.