Home > php教程 > php手册 > php Session存储到Redis的方法

php Session存储到Redis的方法

WBOY
Release: 2016-06-06 20:26:46
Original
1121 people have browsed it

php Session可以保存到文本或者内存、还有数据库,今天讲的是存到Redis的方法。

当然要写先安装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());
 ?>
 

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template