About the problem of too many session files in PHP and the location of the session file Save _php instance

Source: Internet
Author: User
Tags fpm session id php session sessions

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

First, what is the session file

The file is generally/tmp/sessions/sess_4b1e384ad74619bd212e236e52a5a174if

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

Second, when the session recovery occurs

By default, each time a PHP request, there will be 1/100 of the probability of recycling, so it may be simply understood as "every 100 times PHP request has a recycle." This probability is controlled by the following parameters

#概率是gc_probability/gc_divisor
session.gc_probability = 1

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

If your session uses another place in Session.save_path to save the session,session recycle mechanism, it is possible that the expired session file will not be processed automatically. This requires a timed manual (or crontab) Delete expired session

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

#!/bin/sh
find/tmp/php-session-cmin +24-name "sess_*"-and-size 0-delete >/dev/null 2>&1

Here the time we can get through session.gc_maxlifetime, put in the timing task can (crontab)

Other ways

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

Use cookies, but cookies are encrypted

V. Using TMFS storage session

1. Mount/tmp mounted as TMPFS file system

Modify/etc/fstab and 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 a few scripts for creating folders in the source file. The following script is also useful and the script reads as follows

#!/bin/sh
dir= "0 1 2 3 4 5 6 7 8 9 a B c D E F" for
Levela in $dir;
Do with
Levelb in $dir;
Do with
LEVELC in $dir;
Do
mkdir-p/tmp/sessions/$levela/$levelb/$LEVELC;
Done
;
Done

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

3, the session stored in a different directory

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

 
 

4, the session of the recovery

You can use the above script

Next to everyone say the PHP session data saved where?

Of course it's on the server side, but not in memory, but in a file or database.

By default, the session Save method set in php.ini is files (Session.save_handler = files), which saves session data in a read-write file, while the session file is saved in a directory by SESSION.S AVE_PATH specifies that the filename is prefixed with sess_, followed by the session ID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the session data after serialization.

If the visit is large, may produce the session file will be more, at this time can set the hierarchical directory to save the session file, the efficiency will be improved a lot, set the method is: session.save_path= "N;/save_path", N for graded series, save _path is the start directory.

When the session data is written, PHP obtains the session_id of the client, and then finds the corresponding session file in the directory of the specified session file, which is not present, and then the data is serialized and then written to the file. Reading session data is similar to the operation process, to read out the data needs to be serialized, generate the corresponding session variables.

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.