Created
May 29, 2015 08:03
-
-
Save liujingyu/91d8a16fc53dda7d0fcc to your computer and use it in GitHub Desktop.
php redis session 配置
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.修改php.ini中session配置: | |
ini_set('session.save_handler', 'redis'); | |
ini_set('session.save_path', 'tcp://192.168.1.10:6379'); | |
//redis有密码的话 | |
//ini_set('session.save_path', 'tcp://192.168.1.10:6379?auth=password'); | |
注意:php-fpm.conf中的配置会覆盖php.ini中的配置,所以要确保php-fpm中的对应配置关闭,或者修改: | |
2.代码中使用session: | |
<?php | |
/* | |
*可以配置如下选项 | |
*session.auto_start = 1 --自动开始,不需要session_start() | |
*session.cookie_lifetime = 31536000 --cookies中的session_id一年才过期,默认是0,关闭浏览器就过期。 | |
*session.gc_maxlifetime = 1447 --session回收时间,默认1447,存贮在redis中的session的expire被设置为此项。 | |
*/ | |
session_start(); // 可以配置session.auto_start = 1 ,自动开启。 | |
$_SESSION['foo'] = 'bar'; | |
echo $_SESSION['foo']; //bar, 当前的session已经存在redis中 | |
//redis中以string类型存储session | |
$redis = new Redis(); | |
$redis->connect('localhsot', 6379); | |
echo $redis->get( 'PHPREDIS_SESSION:'.session_id() ); | |
//格式:string(29) "uid|i:554:"";username|s:5:"hello";" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment