There are too many Session files in PHP,
Default PHP mechanism: Each php request has a 1/100 probability (default) to trigger "session reclaim ". If "session Recycle" occurs, the/tmp/sess _ * file will be checked. If the last modification time exceeds 1440 seconds (the value of gc_maxlifetime ), delete it, which means that these sessions expire
I. What is a session file?
The file is usually/tmp/sessions/sess_4b1e384ad74619bd212e236e52a5a174If
username|s:9:"test";admin|s:1:"0";
Ii. When session recovery occurs
By default, every php request will have a 1/100 probability of recovery, so it may be simply understood as "every 100 php requests will be recycled once ". This probability is controlled by the following parameters:
# The probability is gc_probability/gc_divisorsession.gc_probability = 1session. gc_divisor = 100.
Assume that gc_maxlifetime is 120. If the last modification time of a session file is 120 seconds ago, the session is still valid until the next recovery (1/100 probability) occurs.
If your session is saved elsewhere in session. save_path, the session recycle mechanism may not automatically process expired session files. In this case, you need to manually (or crontab) delete expired sessions regularly.
3. Set the session storage directory
Modify with php5-fpm/etc/php5/fpm/php.ini
, Modify or add the following line:
session.save_path = 3;600:/tmp/sessions
Iv. session clearing script
#!/bin/shfind /tmp/php-session -cmin +24 -name "sess_*" -and -size 0 -delete > /dev/null 2>&1find /tmp/php-session -cmin +1440 -name "sess_*" -delete > /dev/null 2>&1
The time here can be obtained through session. gc_maxlifetime,Put it in the scheduled task (crontab)
Other methods
Use memcache and so on (session. save_handler = memcache)
Use cookie, but the cookie must be encrypted
5. Use tmfs to store session1. Mount/tmp to the tmpfs file system.
Modify/etc/fstab and add the following content in the last line:/tmp/sessions tmpfs defaults, size = 5120 m 0 0
mount -a
2. Create a session storage folder
Php does not automatically create these folders, but some scripts for creating folders are provided in the source file. The script below is also useful. The script content is as follows:
#!/bin/shdir="0 1 2 3 4 5 6 7 8 9 a b c d e f"for levela in $dir;do for levelb in $dir; do for levelc in $dir; do mkdir -p /tmp/sessions/$levela/$levelb/$levelc; done done;donechown -R root:webgrp /tmp/sessions && chmod -R 1777 /tmp/sessions
Because/tmp/sessions is the memory used, after the server is restarted, all files in it will be lost. Therefore, you need to add the above script to/etc/rc. local, and before starting php
3. Store sessions in different directories
Php itself supports multi-level hash of sessions. in php. ini, change; session. save_path =/tmp
session.save_path = "3;/tmp/sessions
4. session recycling
Use the above script.