当前位置:网站首页>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 :
边栏推荐
- C language games - three chess
- 电子游戏的核心原理
- Gui Gui programming (XIII) - event handling
- 全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
- Dynamically switch data sources
- 过程化sql在定义变量上与c语言中的变量定义有什么区别
- [DIY]如何制作一款个性的收音机
- [DSP] [Part 1] start DSP learning
- R language visualizes the relationship between more than two classification (category) variables, uses mosaic function in VCD package to create mosaic plots, and visualizes the relationship between tw
- Xcode6 error: "no matching provisioning profiles found for application"
猜你喜欢
I've seen many tutorials, but I still can't write a program well. How can I break it?
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
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
Utilisation de l'écran OLED
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
use. Net drives the OLED display of Jetson nano
Design your security architecture OKR
Why do novices often fail to answer questions in the programming community, and even get ridiculed?
Performance test process and plan
Swagger UI教程 API 文档神器
随机推荐
Web开发小妙招:巧用ThreadLocal规避层层传值
[DIY]自己设计微软MakeCode街机,官方开源软硬件
2022 nurse (primary) examination questions and new nurse (primary) examination questions
How does kubernetes support stateful applications through statefulset? (07)
Spark SQL chasing Wife Series (initial understanding)
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
use. Net drives the OLED display of Jetson nano
Redis insert data garbled solution
[DIY]如何制作一款个性的收音机
2022 portal crane driver registration examination and portal crane driver examination materials
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
【每周一坑】正整数分解质因数 +【解答】计算100以内质数之和
Implementation of packaging video into MP4 format and storing it in TF Card
c#使用oracle存储过程获取结果集实例
What key progress has been made in deep learning in 2021?
Taylor series fast Fourier transform (FFT)
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
[weekly pit] output triangle
【微信小程序】運行機制和更新機制
Mtcnn face detection