Redis配置session时,出现问题。Windows的项目可以访问redis服务器,并且session能够存入。但是linux上部署的项目,session是存储不进去的。其他缓存可以存进去。
首先linux是使用的php7,而我本地是使用php5,怀疑是版本不同导致的。因为那个redis.class.php是比较老的,不适应php7。
并且确实有这个问题。
调试过程中,发现session_set_save_handler 返回false。说明我的redis.class.php文件中有错误。更换为thinkphp5 集成的redis.class.php,成功。
调试过程浪费比较长时间,因为本地可以成功,并没有考虑是redis.class.php的问题。不能过度信任,php版本之间差距还是挺大的。
更换成功之后,php7 和 php5 版本都可适用。
(reids服务器配置 注意事项)
如果你配置的redis服务器,有密码。那么去项目链接redis服务器的时候,肯定会出错。
因为thinkphp集成的cache下面的redis.class.php的类中,并没有提供有密码连接的方法。
可以自己添加上这个功能。
ThinkPHP\Library\Think\Cache\Driver\Redis.class.php
原来的类中的连接方法如下。
public function __construct($options=array()) {
if ( !extension_loaded('redis') ) {
E(L('_NOT_SUPPORT_').':redis');
}
$options = array_merge(array (
'host' => C('REDIS_HOST') ? : '127.0.0.1',
'port' => C('REDIS_PORT') ? : 6379,
'timeout' => C('DATA_CACHE_TIMEOUT') ? : false,
'persistent' => false,
),$options);
$this->options = $options;
$this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
$this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
$this->options['length'] = isset($options['length'])? $options['length'] : 0;
$func = $options['persistent'] ? 'pconnect' : 'connect';
$this->handler = new \Redis;
$options['timeout'] === false ?
$this->handler->$func($options['host'], $options['port']) :
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
}
给他添加上密码功能。修改为如下。 public function __construct($options=array()) {
if ( !extension_loaded('redis') ) {
E(L('_NOT_SUPPORT_').':redis');
}
$options = array_merge(array (
'host' => C('REDIS_HOST') ? : '127.0.0.1',
'port' => C('REDIS_PORT') ? : 6379,
'timeout' => C('DATA_CACHE_TIMEOUT') ? : false,
'persistent' => false,
'auth' => C('REDIS_AUTH'),//在这里获取配置的密码
),$options);
$this->options = $options;
$this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
$this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
$this->options['length'] = isset($options['length'])? $options['length'] : 0;
$func = $options['persistent'] ? 'pconnect' : 'connect';
$this->handler = new \Redis;
$options['timeout'] === false ?
$this->handler->$func($options['host'], $options['port']) :
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
//在这里添加密码连接功能
if ('' != $options['auth']) {
$this->handler->auth($options['auth']);
}
}
以上这些问题都是在thinkphp3.2.3 中发现的,说明thinkphp3.2.3还没有准备好使用redis ,而在thinkphp5中我并没有发现这些问题。并且thinkphp5中也已经集成了session的redis.class.php不需要自己写了。所以我觉得要是想使用redis,建议使用thinkphp5最佳答案
