当前位置:网站首页>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 :
边栏推荐
- 小孩子學什麼編程?
- Common doubts about the introduction of APS by enterprises
- [asp.net core] set the format of Web API response data -- formatfilter feature
- What is the difference between procedural SQL and C language in defining variables
- No Yum source to install SPuG monitoring
- C language operators
- [wechat applet] operation mechanism and update mechanism
- Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
- 电子游戏的核心原理
- Boder radius has four values, and boder radius exceeds four values
猜你喜欢
The mail command is used in combination with the pipeline command statement
Distributed ID
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
设计你的安全架构OKR
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
Utilisation de l'écran OLED
What is the problem with the SQL group by statement
面试官:Redis中有序集合的内部实现方式是什么?
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
小孩子學什麼編程?
随机推荐
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
Spark SQL chasing Wife Series (initial understanding)
Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
Yyds dry goods count re comb this of arrow function
强化学习-学习笔记5 | AlphaGo
In line elements are transformed into block level elements, and display transformation and implicit transformation
2022 nurse (primary) examination questions and new nurse (primary) examination questions
2022 portal crane driver registration examination and portal crane driver examination materials
c#使用oracle存储过程获取结果集实例
What are RDB and AOF
Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
解剖生理学复习题·VIII血液系统
知识图谱构建流程步骤详解
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Review questions of anatomy and physiology · VIII blood system
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
Rhcsa Road
7. Data permission annotation
Common doubts about the introduction of APS by enterprises