About the problem of too many session files in PHP and the session file save location, _php tutorial

Source: Internet
Author: User
Tags file upload progress bar php session

About the problem of too many session files in PHP and the session file save location,


PHP default mechanism: Every PHP request, there will be a 1/100 probability (the default) trigger "session recycling." If "Session recycling" occurs, then the/tmp/sess_* file is checked, and if the last modification time is now more than 1440 seconds (gc_maxlifetime value), it will be deleted, which means that the session expiration expires

I. What is the session file?

Files are generally/tmp/sessions/sess_4b1e384ad74619bd212e236e52a5a174if

Username|s:9: "Test"; Admin|s:1: "0";

Second, when the session recovery occurs

By default, every PHP request will have a 1/100 probability of recycling, so it might simply be understood as "one recovery per 100 PHP requests". This probability is controlled by the following parameters

Assuming this situation is gc_maxlifetime=120, if the last modification time of a session file is 120 seconds ago, the session will still be valid until the next recovery (probability of 1/100) occurs.

If your session uses Session.save_path to save the session,session recycle mechanism, it is possible that expired session files will not be processed automatically. It is necessary to periodically delete the expired session manually (or crontab)

Third, set the session storage directory

With PHP5-FPM, modify the/etc/php5/fpm/php.ini, modify or add the following line:

Iv. Session Cleanup Script

The time here we can get through the session.gc_maxlifetime, put in the scheduled task can be (crontab)

Other ways

Use Memcache, etc., (Session.save_handler = memcache)

Use cookies, but cookies are encrypted

V. Using TMFS storage session

1. Mount/tmp as TMPFS file system

To modify/etc/fstab, add the following on the last line:/tmp/sessions tmpfs defaults,size=5120m 0 0

2. Create Session Storage Folder

PHP does not automatically create these folders, but it provides some script for creating folders in the source file. The following script is also useful, the script content is as follows

Because/tmp/sessions is used memory, after the server restarts, all the files inside will be lost, so you need to add the above script to/etc/rc.local, and put it before starting PHP

3. Store the session in a different directory

PHP itself supports the multi-level hash of the session, in php.ini, Session.save_path =/tmp to

4, the session of the recovery

Use the script above to

The following is to tell you where the PHP session data is saved?

It is, of course, on the server side, but not in memory, but in a file or database.

By default, the session set in PHP.ini is saved by files (Session.save_handler = files), that is, the session data is saved using a read-write file, and the directory saved by the session file is SESSION.S AVE_PATH specifies that the file name is prefixed with Sess_, followed by the SESSION ID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the SESSION data after the serialization.

If the access volume is large, may produce the session file will be more, then you can set the hierarchical directory to save the session file, the efficiency will be improved a lot, the setting method is: Session.save_path= "N;/save_path", N is a graded series, save _path is the starting directory.

When the session data is written, PHP will obtain the client's session_id, and then according to the session ID to the specified session file to save the directory to find the corresponding session file, does not exist to create, and finally to serialize the data to write to the file. Reading session data is a similar process, and the data that is read needs to be deserialized to generate the corresponding session variables.

Articles you may be interested in:

    • Detailed Setup parameter description for session in PHP5 configuration file
    • How to use session and cookie to save user login information in PHP
    • PHP implements the method of saving the session using MySQL
    • PHP using session with JavaScript to implement the file Upload progress bar function
    • PHP Custom File Save Session Method
    • Workaround for blocking problems caused by exclusive lock of PHP session file
    • PHP using session and file statistics online number of people

http://www.bkjia.com/PHPjc/1111335.html www.bkjia.com true http://www.bkjia.com/PHPjc/1111335.html techarticle about the problem of too many session files in PHP and the session file save location, PHP default mechanism: Every time PHP request, there will be a 1/100 probability (the default) trigger "session recycling." If ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.