有两种方法
第一种:通过php.ini配置
在session_start()之前增加如下代码:
ini_set('session.save_handler', 'redis');
ini_set('session.save_path', 'tcp://127.0.0.1:6379');
或者修改php.ini这两处为上面的值重启服务即可
第二种:通过session_set_save_handler
函数
根据官方手册session驱动增加下面的驱动文件
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
namespace Think\Session\Driver;
// use think\Exception;
class Redis implements \SessionHandlerInterface
{
/** @var \Redis */
protected $handler = null;
protected $config = [
'host' => '127.0.0.1', // redis主机
'port' => 6379, // redis端口
'password' => '', // 密码
'select' => 0, // 操作库
'expire' => 3600, // 有效期(秒)
'timeout' => 0, // 超时时间(秒)
'persistent' => true, // 是否长连接
'session_name' => 'session_', // sessionkey前缀
];
public function __construct($config = [])
{
$this->config['host'] = C("SESSION_REDIS_HOST") ? C("SESSION_REDIS_HOST") : $this->config['host'];
$this->config['port'] = C("SESSION_RE