关于PHP中Session文件过多的问题,
PHP的默认机制:每一次php请求,会有1/100的概率(默认值)触发“session回收”。如果“session回收”发生,那就会检查/tmp/sess_*的文件,如果最后的修改时间到现在超过了1440秒(gc_maxlifetime的值),就将其删除,意味着这些session过期失效
一、session文件是什么
文件一般为 /tmp/sessions/sess_4b1e384ad74619bd212e236e52a5a174If
username|s:9:"test";admin|s:1:"0";
二、session回收何时发生
默认情况下,每一次php请求,就会有1/100的概率发生回收,所以可能简单的理解为“每100次php请求就有一次回收发生”。这个概率是通过以下参数控制的
#概率是gc_probability/gc_divisor session.gc_probability = 1 session.gc_divisor = 100
-
假设这种情况gc_maxlifetime=120,如果某个session文件最后修改时间是120秒之前,那么在下一次回收(1/100的概率)发生前,这个session仍然是有效的。
-
如果你的session使用session.save_path中使用别的地方保存session,session回收机制有可能不会自动处理过期session文件。这时需要定时手动(或者crontab)的删除过期的session
三、设置session存储目录
使用php5-fpm的话, 修改/etc/php5/fpm/php.ini
, 修改或添加下面一行:
session.save_path = 3;600:/tmp/sessions
四、session清除脚本
#!/bin/sh find /tmp/php-session -cmin +24 -name "sess_*" -and -size 0 -delete > /dev/null 2>&1 find /tmp/php-session -cmin +1440 -name "sess_*" -delete > /dev/null 2>&1
这里的时间我们可以通过 session.gc_maxlifetime 来获取,放到定时任务里面即可(crontab)
其他方式
-
使用memcache 等 ,(session.save_handler = memcache)
-
使用cookie,但是cookie得加密
五、使用tmfs存储session
1、将/tmp挂载为 tmpfs文件系统
修改/etc/fstab,在最后一行添加如下内容:/tmp/sessions tmpfs defaults,size=5120m 0 0
mount -a
2、创建session存储文件夹
php并不会自动去创建这些文件夹,不过在源文件中提供了一些创建文件夹的脚本。下面这个脚本也好用,脚本内容如下
#!/bin/sh dir="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; done chown -R root:webgrp /tmp/sessions && chmod -R 1777 /tmp/sessions
因为/tmp/sessions是用的内存,服务器重启后,里面的所有文件都会丢失,所以,需要把上面的脚本加入到 /etc/rc.local中,并且要放在启动php之前
3、将session存储到不同的目录中
php本身支持session的多级散列,在php.ini中,将 ;session.save_path = /tmp 改为
session.save_path = "3;/tmp/sessions
4、session的回收
使用上面的脚本即可

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development 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.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft