Session is a very commonly used mechanism in PHP. It saves the data generated by the user during the visit to the website to the server so that the data can be used in subsequent operations. For example, in a shopping website, after the user adds items to the shopping cart, he or she can use the shopping cart data saved in the Session on the checkout page.
However, in PHP, Session is stored on the server in the form of a file by default. Although this method is simple, it has some potential problems. For example, if the website has a high number of visits, a large number of Session files will be generated on the server, which can easily cause server performance problems. Moreover, since Sessions are stored locally on the server, they will also be affected by problems such as server crashes and Session files being deleted. .
Therefore, in order to improve the stability and performance of Session, it is necessary for us to make some adjustments to Session. Below, we take PHP 7.2 version as an example to introduce how to modify the PHP configuration of Session.
In PHP, session.save_handler is the name of the Session storage processor. You can adjust the method of Session storage by modifying this parameter.
For high-traffic websites, we can set session.save_handler to redis or memcached to store the Session in the cache. This can significantly reduce the number of Session files on the server and improve server performance.
The following code example demonstrates how to store a Session in redis:
session_save_path('tcp://127.0.0.1:6379?database=0'); ini_set('session.save_handler', 'redis');
In PHP , session.gc_probability and session.gc_divisor are parameters of the Session garbage collection mechanism. They are used to control the frequency of Session recycling.
By default, the value of session.gc_probability is 1, which means that each request has a certain probability of triggering Session recycling; and the value of session.gc_divisor is 100, which means that each request has a certain probability of triggering Session recycling. The probability of triggering Session recycling is one. This will make garbage collection very frequent and affect server performance.
Therefore, we can appropriately adjust the values of session.gc_probability and session.gc_divisor to reduce the number of triggers of the Session recycling mechanism.
The following code example demonstrates how to set the values of session.gc_probability and session.gc_divisor to larger values, thereby reducing the number of triggers of the recycling mechanism:
ini_set('session.gc_probability', 50); ini_set('session.gc_divisor', 1000);
In PHP, session.gc_maxlifetime is the parameter of Session life cycle. It indicates the maximum storage time of Session files. Session files that exceed this time will be automatically deleted.
By default, the value of session.gc_maxlifetime is 1440 seconds (24 minutes), which means that the Session file can only survive for 24 minutes. For some applications that need to store the Session for a long time, this time may be too short, causing the Session to be lost.
Therefore, we can appropriately extend the Session survival time by modifying the session.gc_maxlifetime parameter.
The following code example demonstrates how to set the value of session.gc_maxlifetime to 3600 seconds (1 hour):
ini_set('session.gc_maxlifetime', 3600);
In PHP, session.cookie_lifetime is the life cycle parameter of Session Cookie. It represents the survival time of the server-side Session ID in the user-side cookie. When this time expires, the user needs to regenerate a new Session ID.
By default, the value of session.cookie_lifetime is 0, which means that the Session Cookie will expire after the user closes the browser. In this way, in some applications that need to store Session for a long time, the user needs to log in again after closing the browser.
Therefore, we can extend the survival time of Session Cookie and maximize the role of Session by modifying the session.cookie_lifetime parameter.
The following code example demonstrates how to set the value of session.cookie_lifetime to 3600 seconds (1 hour):
ini_set('session.cookie_lifetime', 3600);
Summary
By adjusting the PHP configuration of Session, we can Optimize and strengthen Session to improve server performance and stability. The PHP configurations introduced above can be used as a reference, and developers can adjust them according to their actual needs.
The above is the detailed content of How to modify session configuration in php. For more information, please follow other related articles on the PHP Chinese website!