Home  >  Article  >  Backend Development  >  What to do if php session doesn’t work

What to do if php session doesn’t work

藏色散人
藏色散人Original
2020-08-22 09:17:322794browse

The solution to the problem that php session does not work: First call the "session_save_path()" function at the beginning of the page; then point the directory where the session is saved to a dedicated directory.

What to do if php session doesn’t work

Recommendation: "PHP Video Tutorial"

Cause of php Session failure

Recently, a problem of session failure occurred in the company's project. After the program was run, the session failed within a few minutes. Later, I read it online for a long time and understood its mechanism: Session is stored in a public path in Lunix, which will cause a problem. If there are many Session applications in the program, they will affect each other.

Later I tried to modify session.gc_maxlifetime but I found that this parameter basically did not work. By default, session information in PHP is saved in the system's temporary file directory in the form of text files. This path is specified by the configuration parameter session.save_path. Under Linux, this path is usually \tmp, and under Windows it is usually C:\Windows\Temp. When there are multiple PHP applications on the server, they will save their session files in the same directory (because they use the same session.save_path parameter). Similarly, these PHP applications will also start GC at a certain probability and scan all session files.

The problem is that when the GC is working, it does not distinguish between sessions on different sites. For example, site A's gc_maxlifetime is set to 2 hours, and site B's gc_maxlifetime is set to the default 24 minutes. When the GC of site B starts, it will scan the public temporary file directory and delete all session files older than 24 minutes, regardless of whether they come from site A or B. In this way, the gc_maxlifetime setting of site A is useless.

So the specific operation is that each project has its own session path. It is very simple to find the problem and solve it. Call the session_save_path() function at the beginning of the page, which can modify the session.save_path parameter and point the directory where the session is saved to a dedicated directory, such as \tmp\myapp\. In this way, the gc_maxlifetime parameter works normally.

Specific code:

session_start();
ini_set('session.save_path','/tmp/');
//6个钟头
ini_set('session.gc_maxlifetime',21600);
//保存一天
$lifeTime = 24 * 3600;
setcookie(session_name(), session_id(), time() + $lifeTime, "/");

The above is the detailed content of What to do if php session doesn’t work. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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