欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

数据库储存session信息代码,储存session代码_PHP教程

程序员文章站 2024-02-04 08:56:46
...

数据库储存session信息代码,储存session代码

今天给大家上一段代码,数据库存储session信息,你只需要将下面这段代码放到session文件中,然后再session_start()的地方引入sessiong文件就行啦,当然你就不用再写session_start()啦

这是数据库表的结构

数据库储存session信息代码,储存session代码_PHP教程

好,下面上代码啦

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.htmlTechArticle数据库储存session信息代码,储存session代码 今天给大家上一段代码, 数据库存储session信息 ,你只需要将下面这段代码放到session文件中,然...
相关标签: session