Home  >  Article  >  Backend Development  >  php Session存储到Redis的方法_php技巧

php Session存储到Redis的方法_php技巧

WBOY
WBOYOriginal
2016-05-17 08:53:461028browse

当然要写先安装php的扩展,可参考这篇文章:Redis及PHP扩展安装
修改php.ini的设置

复制代码 代码如下:

session.save_handler = redis
session.save_path = “tcp://127.0.0.1:6379″
修改后重启php-fpm或nginx,phpinfo()

session redis
如果不想修改php.ini可这样

复制代码 代码如下:

ini_set(“session.save_handler”,”redis”);
ini_set(“session.save_path”,”tcp://127.0.0.1:6379″);

如果配置文件/etc/redis.conf里设置了连接密码requirepass保存session的时候会报错,save_path这样写 tcp://127.0.0.1:6379?auth=authpwd 即可。
有网友提到相对于文件存储的session,redis或者memcache存储session值会有并发一致性的问题,这块并没有细测过。
复制代码 代码如下:

//如果未修改php.ini下面两行注释去掉
//ini_set('session.save_handler', 'redis');
//ini_set('session.save_path', 'tcp://127.0.0.1:6379');
session_start();
$_SESSION['sessionid'] = 'this is session content!';
echo $_SESSION['sessionid'];
echo '
';

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
//redis用session_id作为key并且是以string的形式存储
echo $redis->get('PHPREDIS_SESSION:' . session_id());
 ?>
 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn