<?php
ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://localhost:6379");
session_start();
header("Content-type:text/html;charset=utf-8");
if(isset($_SESSION['view'])){
$_SESSION['view'] = $_SESSION['view'] + 1;
}else{
$_SESSION['view'] = 1;
}
echo "【view】{$_SESSION['view']}";
//这里设置session.save_handler方式为redis,session.save_path为redis的地址和端口,设置之后刷新,再回头查看redis,会发现redis中的生成了sessionId,sessionId和浏览器请求的是一样的,
//也可以使用
Session_set_save_handler(‘open','close',' read',' write',' destory',' gc');
//用法如下自定义一个Redis_session类
<?php
class RedisSession{
private $_redis = array(
'handler' => null, //数据库连接句柄
'host' => null, //redis端口号
'port' => null,
);
public function __construct($array = array()){
isset($array['host'])?$array['host']:"false";
isset($array['port'])?$array['host']:"false";
$this->_redis = array_merge($this->_redis, $array);
}
public function begin(){
//设置session处理函数
session_set_save_handler(
array($this, 'open'),
array($this, 'close'),
array($this, 'read'),
array($this, 'write'),
array($this, 'destory'),
array($this, 'gc')
);
}
public function open(){
$redis = new Redis();
$redis->connect($this->_redis['host'], $this->_redis['port']);
if(!$redis){
return false;
}
$this->_redis['handler'] = $redis;
$this->gc(null);
return true;
}
//关
public function close(){
return $this->_redis['handler']->close();
}
//读
public function read($session_id){
return $this->_redis['handler']->get($session_id);
}
//写
public function write($sessionId, $sessionData){
return $this->_redis['handler']->set($sessionId, $sessionData);
}
public function destory($sessionId){
return $this->_redis['handler']->delete($sessionId) >= 1 ? true : false;
}
public function gc(){
//获取所有sessionid,让过期的释放掉
$this->_redis['handler']->keys("*");
return true;
}
}
$ses = new RedisSession(array('host'=>'127.0.0.1','port'=>'6379'));
$ses->begin();
session_start();
$_SESSION['name']='zhangsan';
echo $_SESSION['name'];
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有