Home > Backend Development > PHP Tutorial > The database stores session information code and stores session code_PHP tutorial

The database stores session information code and stores session code_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 09:51:06
Original
855 people have browsed it

The database stores session information code, stores session code

Today I will give you a piece of code, The database stores session information, you only need to add the following code Just put it in the session file, and then import the sessiong file where session_start() is. Of course, you don’t need to write session_start() anymore

This is the structure of the database table

Okay, here’s the code

class session {
private static $_mysqli;

public static function action() {
ini_set('session.save_handler','user');
session_set_save_handler(array(__CLASS__,'open'),
array(__CLASS__,'close' ),
array(__CLASS__,'read'),
array(__CLASS__,'write'),
array(__CLASS__,'destroy'),
array(__CLASS__,'gc')) ;
@session_start();
}

public static function open($path,$name) {
self::$_mysqli = new mysqli('localhost','root' ,'','test');
return true;
}

public static function close() {
self::$_mysqli->close();
return true;
}

public static function read($sid) {
$_query = "SELECT sdata FROM session WHERE sid='{$sid}' LIMIT 1";
$ _result = self::$_mysqli->query($_query);
$_sdata = $_result->fetch_object();
return $_sdata->sdata;
}

public static function write($sid,$sdata) {
$_query = "SELECT sid FROM session WHERE sid='{$sid}' LIMIT 1";
$_result = self::$_mysqli- >query($_query);
if (!!$_sid = $_result->fetch_object()) {
$_query = "UPDATE session SET sdata='{$sdata}' WHERE sid=' {$sid}'";
self::$_mysqli->query($_query);
} else {
$_query = "INSERT INTO session (sid,sdata) VALUES ('{$ sid}','{$sdata}')";
self::$_mysqli->query($_query);
}
return true;
}

public static function destroy($sid) {
$_query = "DELETE FROM session WHERE sid='{$sid}' LIMIT 1";
self::$_mysqli->query($_query);
setCookie(ini_get('session.name'),'',time()-1);
return true;
}

public static function gc($maxlifetime) {
$_query = "DELETE FROM session WHERE NOW()-slasttime>'{$maxlifetime}'";
self::$_mysqli->query($_query);
return true;
}
}

session::action();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1014828.htmlTechArticleThe database stores session information code. Store session code. Today I will give you a piece of code. The database stores session information. You only need to Put the following code into the session file, then...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Session
From 1970-01-01 08:00:00
0
0
0
session
From 1970-01-01 08:00:00
0
0
0
session login information
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template