Home > Backend Development > PHP Tutorial > How to write php session to redis

How to write php session to redis

小云云
Release: 2023-03-19 16:14:01
Original
2180 people have browsed it

Session: In computers, especially in network applications, it is called "session control". So this article will share with you how to save the session to redis. If you need it, hurry up and learn it.

PHP's session session is saved as a file in the session cache directory set in the php.ini configuration file by default. The efficiency of file saving session is very low. Every time each user logs in, a session will be generated on the server. The only session_id file. When the number of user logins reaches hundreds of thousands, hundreds of thousands of files will be generated on the server. At this time, the IO reading and writing of the session directory on the disk will be very slow, causing the program to fail when the user logs in. Spend a lot of time reading and writing session files. When a large number of users log in, the read and write capabilities are insufficient. As a result, the file will be locked by the first process that operates the session, and other requests will be blocked. The request will be suspended in session_start() until the session file is unlocked. At this time, it is necessary to optimize the slow login problem.

Since the locked session file will not be unlocked until the script execution ends or the session is closed normally, in order to prevent a large number of php requests (which require the use of $_SESSION data) from being locked, you can close the session immediately after writing it. , thus releasing the lock.

//开启会话
session_start();
//写入会话
$_SESSION['is_login']=1;
//写入会话后关闭上一个会话文件的写入
session_write_close();
Copy after login

php session When the session is written to redis

redis data is stored and run in the computer memory stick. The speed of the computer's memory stick is dozens of times that of the hard disk, so if you put php The session is written to the redis cache, and the speed will be greatly improved. Configuration method:

//方式一,在php脚本中设置更改php会话写入方式
<?php
ini_set("session.save_handler","redis"); 
ini_set("session.save_path","tcp://127.0.0.1:6379");
//如果有密码
ini_set("session.save_path","tcp://127.0.0.1:6379?auth=password");
?>
Copy after login
//修改php的配置文件php.ini
session.save_handler = redis 
session.save_path = "tcp://127.0.0.1:6379"
//如果有密码
session.save_path = "tcp://127.0.0.1:6379?auth=password"
Copy after login

Related recommendations:

How to register and read Session in php

The above is the detailed content of How to write php session to redis. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template