Home > Backend Development > PHP Tutorial > php Redis storage session

php Redis storage session

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:12:36
Original
1232 people have browsed it

Please support for more: http://www.webyang.net/Html/web/article_180.html

Redis is a high-performance key-value database. The emergence of redis can play a very good supplementary role to relational databases in some situations. On the other hand, many people choose redis for session storage. It compensates for the speed and efficiency of file storage to achieve a higher availability.

Modify the settings of php.ini

<ol>
<li value="1">
<span>session</span><span>.</span><span>save_handler </span><span>=</span><span> redis</span>
</li>
<li>
<span>session</span><span>.</span><span>save_path </span><span>=</span><span>"tcp://127.0.0.1:6379"</span>
</li>
</ol>
Copy after login
Restart php-fpm after modification. phpinfo() can see that the session is stored in redis.
If you don’t want to modify php.ini, you can add:
<ol>
<li value="1">
<span>ini_set</span><span>(</span><span>"session.save_handler"</span><span>,</span><span>"redis"</span><span>);</span>
</li>
<li>
<span>ini_set</span><span>(</span><span>"session.save_path"</span><span>,</span><span>"tcp://127.0.0.1:6379"</span><span>);</span>
</li>
</ol>
Copy after login
to the code. If redis.conf sets a connection password (requirepass), the save_path of the session needs to be modified to: tcp://127.0.0.1:6379?auth= requirepass value.
If you choose redis database, session.save_path = "tcp://xx.xx.xx.xx:6789?database=11", etc.
Check the value of the redis storage session:
<ol>
<li value="1">
<span><?</span><span>php</span></li><li><span>session_start</span><span>();</span></li><li><span>$_SESSION</span><span>[</span><span>'sessionid'</span><span>]</span><span>=</span><span>'www.webyang.net'</span><span>;</span></li><li><span> </span></li><li><span>$redis </span><span>=</span><span>new</span><span> redis</span><span>();</span></li><li><span>$redis</span><span>-></span><span>connect</span><span>(</span><span>'127.0.0.1'</span><span>,</span><span>6379</span><span>);</span>
</li>
<li><span>//redis用 PHPREDIS_SESSION: 加 session_id 作为key,并且是以string的形式存储</span></li>
<li>
<span>echo $redis</span><span>-></span><span>get</span><span>(</span><span>'PHPREDIS_SESSION:'</span><span>.</span><span> session_id</span><span>());</span><span>//输出www.webyang.net</span>
</li>
<li><span>?></span></li>
</ol>
Copy after login

I saw on the Internet that some people questioned the concurrency consistency problem of redis storage session value (file storage session is handled by file locks), but there is no research on this.
Linux installation redis service and php redis extension: www.webyang.net/Html/web/article_174.html


The above introduces the PHP Redis storage session, including the require content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Latest Issues
Session
From 1970-01-01 08:00:00
0
0
0
session
From 1970-01-01 08:00:00
0
0
0
session login information
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template