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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
