## 实现接口类SessionHandlerInterface
~~~
<?php
/**
* Created by gather
* Email: chenruiqiang@yd-x.com
* Phone: 16601180687
* Copyright:源动互通(北京)科技有限公司
* Create Time: 2018/5/28 19:12
* 使用Mysql管理session
*/
class CustomSession implements SessionHandlerInterface{
private $link = null; //数据库连接
private $lifetime; //声明周期
/* 方法 */
public function close(){
return mysqli_close($this->link);
}
public function destroy($session_id){
//删除session
$session_id = mysqli_real_escape_string($this->link,$session_id);
$sql = "DELETE FROM sessions WHERE session_id='{$session_id}'";
mysqli_query($this->link,$sql);
return mysqli_affected_rows($this->link) == 1;
}
public function gc($maxlifetime){
$sql = "DELETE FROM sessions WHERE session_expires < ".time();
mysqli_query($this->link,$sql);
return mysqli_affected_rows($this->link) > 0;
}
public function open($save_path,$session_name){
$this->lifetime = get_cfg_var("session.gc_maxlifetime"); //默认24分钟
$this->link = mysqli_connect('localhost','root','xyq07041103');
mysqli_set_charset($this->link,'utf8');
mysqli_select_db($this->link,'email');
if ($this->link){
return true;
}
return false;
}
public function read($session_id){
$id = mysqli_real_escape_string($this->link,$session_id);
$sql = "SELECT * from sessions WHERE session_id='{$id}' AND session_expires > ".time();
$result = mysqli_query($this->link,$sql);
if (mysqli_num_rows($result) == 1){
return mysqli_fetch_assoc($result)['session_data'];
}
return '';
}
public function write($session_id,$session_data){
//首先查询是否存在指定的session_id,如果存在更新数据,否自写入数据
$session_id = mysqli_real_escape_string($this->link,$session_id);
$newExp = time() + $this->lifetime;
$sql = "SELECT * from sessions WHERE session_id='{$session_id}'";
$result = mysqli_query($this->link,$sql);
$rows = mysqli_num_rows($result);
if ($rows == 1){
$sql = "UPDATE sessions SET session_expires='{$newExp}',session_data='{$session_data}' WHERE session_id='{$session_id}'";
}else{
$sql = "INSERT INTO sessions VALUES('{$session_id}','{$session_data}','{$newExp}')";
}
mysqli_query($this->link,$sql);
return mysqli_affected_rows($this->link) == 1;
}
}
~~~
## 创建session
~~~
<?php
/**
* Created by gather
* Email: chenruiqiang@yd-x.com
* Phone: 16601180687
* Copyright:源动互通(北京)科技有限公司
* Create Time: 2018/5/28 20:00
*/
require_once "CustomSession.php";
$CustomSession = new CustomSession();
ini_set('session.save_handler','user'); //自定义管理session
session_set_save_handler($CustomSession,true);
session_start();
$_SESSION['username'] = 'king';
$_SESSION['age'] = '23';
$_SESSION['email'] = 'aaaaaa5474@126.com';
~~~
## 删除session
~~~
<?php
/**
* Created by gather
* Email: chenruiqiang@yd-x.com
* Phone: 16601180687
* Copyright:源动互通(北京)科技有限公司
* Create Time: 2018/5/28 20:22
*/
require_once "CustomSession.php";
$CustomSession = new CustomSession();
ini_set('session.save_handler','user'); //自定义管理session
session_set_save_handler($CustomSession,true);
session_start();
print_r($_SESSION);
session_destroy();
~~~
本地环境测试成功
- 简介
- Cookie
- HTML5 LocalStorage
- session
- 当浏览器关闭后,Session就销毁了吗?
- mysql数据库保存session
- HTTP协议的由来
- fsockopen异步请求
- http防盗链
- Apache伪静态知识补充
- 大并发量解决方案
- 大型网站是怎样解决多用户高并发访问
- 网站高并发 大流量访问的处理及解决方法
- 并发数与在线客户数 注册用户数的关系
- 即时聊天程序
- 反向Ajax实现
- ob缓存作用
- 浅聊并发之战
- php扩展安装
- php安装redis扩展
- SQLMap自动化实施SQL注入共计
- 命名空间namespace
- 集群和分布式之【session共享】
- php Redis存储Session 【1】
- php Redis存储Session 【2】
- php mysql存储session【1】
- php缓存
- 文件缓存
- memcache和redis的比较
- 原生session与session in redis对比
- XSS攻击【1】
- XSS攻击【2】
- PHP消息队列
- php+mysql 模拟发送邮件队列
- php+mysql 模拟订单处理队列
- php+redis 模拟秒杀队列
- RabbitMQ 消息队列系统
- beanstalkd
- PHP构建即时通讯
- WebSocket协议
- workerman
- PHP变量的作用域
- PHP传值和传引用的区别
- PHP匿名函数
- PHP递归函数&应用
- PHP单例模式
- PHP性能优化
- RESTful
- 集群
- 增加pgsql扩展
- php.ini路径查找
- Swoole Compiler
- mysql 主从
- 主从
- mysql-proxy
- window docker环境