💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 实现接口类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(); ~~~ 本地环境测试成功