PHP uses redis to realize session instance sharing

*文
Release: 2023-03-18 12:02:01
Original
1399 people have browsed it

Redis has been widely used due to its efficient performance. Traditional sessions store data in files. Due to the low IO performance of disks, session performance encounters a bottleneck. This article mainly introduces how to store sessions in redis to improve program efficiency.

Method one:

Find the configuration file php.ini, modify it to the following content, save and restart the service

session.save_handler = redis session.save_path = "tcp://127.0.0.1:6379"
Copy after login

Method two:

Directly in the code Add the following content:

ini_set("session.save_handler", "redis"); ini_set("session.save_path", "tcp://127.0.0.1:6379");
Copy after login

Note: If the connection password requirepass is set in the configuration file redis.conf, save_path needs to be written like this tcp://127.0.0.1:6379?auth=authpwd, otherwise when saving the session An error will be reported.


Test:

 'toefl', 'num' => 8); //连接redis $redis = new redis(); $redis->connect('127.0.0.1', 6379); //检查session_id echo 'session_id:' . session_id() . '
'; //redis存入的session(redis用session_id作为key,以string的形式存储) echo 'redis_session:' . $redis->get('PHPREDIS_SESSION:' . session_id()) . '
'; //php获取session值 echo 'php_session:' . json_encode($_SESSION['class']);
Copy after login

Related recommendations:

PHP’s SESSION mechanism analysis

Cookie/Session mechanism introduction

Redis cluster building graphic tutorial

The above is the detailed content of PHP uses redis to realize session instance sharing. For more information, please follow other related articles on the PHP Chinese website!

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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!