当前位置:网站首页>PHP saves session data to MySQL database
PHP saves session data to MySQL database
2022-07-06 20:53:00 【hello php】
session_set_save_handler() Use cases of methods
sql The statement is as follows :
CREATE TABLE `session` (
`id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT,
`data` varchar(255) DEFAULT NULL,
`lapse_time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
The code is as follows :
<?php
/**
* Reference resources : http://c.biancheng.net/view/8192.html
*/
// Connect to database
function sess_open($savePath, $sessionName)
{
echo "sess_open() Method <br/>";
global $link;
$link = mysqli_connect('localhost', 'root', 'root') or die(' Database connection failed !');
mysqli_select_db($link, 'test') or die(' The database was not found !');
return true;
}
// Close database connection
function sess_close()
{
echo "sess_close() Method <br/>";
global $link;
mysqli_close($link);
return true;
}
// Read Session Content
function sess_read($sessionId)
{
echo "sess_read() Method <br/>";
global $link;
$time = time();
$sql = "SELECT data FROM session WHERE id = '$sessionId' AND lapse_time > '$time'";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if ($row) {
// return $row['data'];
return serialize($row['data']);// pit !!! Need to serialize
} else {
return serialize(false);// pit !!! Need to serialize
}
}
// write in Session
function sess_write($sessionId, $data)
{
echo "sess_write() Method <br/>";
global $link;
$lapse_time = time() + (60 * 60);
$sql = "SELECT data FROM session WHERE id = '$sessionId'";
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) == 0) {
$ins_sql = "INSERT INTO session VALUES('$sessionId', '$data', '$lapse_time')";
$res = mysqli_query($link, $ins_sql);
} else {
$up_sql = "UPDATE session SET data = '$data', lapse_time = '$lapse_time' WHERE id = '$sessionId'";
$res = mysqli_query($link, $up_sql);
}
return $res;
}
// Delete Session
function sess_destroy($sessionId)
{
echo "sess_destroy() Method <br/>";
global $link;
$sql = "DELETE FROM session WHERE id = '$sessionId'";
return mysqli_query($link, $sql);
}
// Clean up invalid Session
function sess_gc()
{
echo "sess_gc() Method <br/>";
global $link;
$time = time();
$sql = "DELETE FROM session WHERE lapse_time < '$time'";
$res = mysqli_query($link, $sql);
return $res;
}
//session_set_save_handler('sess_open', 'sess_close', 'sess_read', 'sess_write', 'sess_destroy', 'sess_gc');
//echo ini_get("session.save_handler") . PHP_EOL; // return user
//die;
//**********************************************************************
//ini_set("session.save_handler", "user");//user For custom
session_set_save_handler('sess_open', 'sess_close', 'sess_read', 'sess_write', 'sess_destroy', 'sess_gc');
session_start();
$_SESSION['user'] = 'admin';
var_dump($_SESSION['user']);
The result of the browser visit is as follows :

The database data is as follows :

边栏推荐
- [asp.net core] set the format of Web API response data -- formatfilter feature
- ##无yum源安装spug监控
- Detailed explanation of knowledge map construction process steps
- use. Net drives the OLED display of Jetson nano
- Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
- 设计你的安全架构OKR
- 2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
- What are RDB and AOF
- Why do novices often fail to answer questions in the programming community, and even get ridiculed?
- 'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
猜你喜欢

C language operators

【微信小程序】运行机制和更新机制

No Yum source to install SPuG monitoring

Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache

Detailed introduction of distributed pressure measurement system VIII: basic introduction of akka actor model

How to upgrade high value-added links in the textile and clothing industry? APS to help

硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件

2022 portal crane driver registration examination and portal crane driver examination materials
![[wechat applet] operation mechanism and update mechanism](/img/cf/58a62a7134ff5e9f8d2f91aa24c7ac.png)
[wechat applet] operation mechanism and update mechanism
![[DIY]自己设计微软MakeCode街机,官方开源软硬件](/img/a3/999c1d38491870c46f380c824ee8e7.png)
[DIY]自己设计微软MakeCode街机,官方开源软硬件
随机推荐
【DSP】【第二篇】了解C6678和创建工程
C language games - minesweeping
知识图谱构建流程步骤详解
2110 summary of knowledge points and common problems in redis class
'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
【OpenCV 例程200篇】220.对图像进行马赛克处理
Redis insert data garbled solution
看过很多教程,却依然写不好一个程序,怎么破?
[DIY]自己设计微软MakeCode街机,官方开源软硬件
What is the problem with the SQL group by statement
Rhcsa Road
Trends of "software" in robotics Engineering
Variable star --- article module (1)
OSPF多区域配置
Entity alignment two of knowledge map
Comment faire une radio personnalisée
监控界的最强王者,没有之一!
Value of APS application in food industry
Detailed explanation of knowledge map construction process steps