Home > Backend Development > PHP Tutorial > PHP session 存储方式

PHP session 存储方式

WBOY
Release: 2016-06-23 13:28:13
Original
1046 people have browsed it

file :

session.save_handler = filessession.save_path = "/tmp"
Copy after login

session文件同步


memcache:

session.save_handler = memcachesession.save_path = "tcp://127.0.0.1:11211,tcp://Mem服务器2:端口号..."使用多个 memcached server 时用逗号","隔开,可以带额外的参数"persistent"、"weight"、"timeout"、"retry_interval" 等,类似:"tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2"参考:http://php.net/manual/zh/memcache.addserver.php如果安装的PECL是memcached(使用libmemcache库的那个),则配置应为ini_set("session.save_handler", "memcached"); // 是memcached不是memcacheini_set("session.save_path", "127.0.0.1:11211"); // 不要tcp://参数参考:http://php.net/manual/zh/memcached.addserver.php
Copy after login

参数参考:
http://php.net/manual/zh/memcache.addserver.php
http://php.net/manual/zh/memcached.addserver.php



redis:

session.save_handler = redissession.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2"参数:weight 比重timeout 超时persistent 持久连接 取值 0 1prefix session id 前缀auth 验证database 选择的数据库session 以秒表示 生命周期由session.gc_maxlifetime来控制ini_set('session.gc_maxlifetime','3600')可改变生存时间,需要SETEX 命令,redis版本至少2.0phpredis 也可以链接一个unix domain socket :unix:///var/run/redis/redis.sock?persistent=1&weight=1&database=0
Copy after login
参数参考: https://github.com/phpredis/phpredis   ,


几种方式优缺点很明显:建议使用redis


redis实例:

ini_set('session.save_handler', 'redis');ini_set('session.save_path', 'tcp://127.0.0.1:6379');session_start();$_SESSION['sessionKey'] = 'redis session!';var_dump($_SESSION['sessionKey']);echo '<br>';        $redis = new redis();$redis->connect('127.0.0.1', 6379);//redis用session_id作为key并且是以string的形式存储var_dump($redis->get('PHPREDIS_SESSION:' . session_id()));
Copy after login


版权声明:本文为博主原创文章,未经博主允许不得转载。

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template