Home > php教程 > PHP源码 > PHP设置session定期自动清理的例子

PHP设置session定期自动清理的例子

WBOY
Release: 2016-06-08 17:20:45
Original
1178 people have browsed it

下文来为各位介绍PHP设置session定期自动清理的例子了,因为session默认是15分钟自动把变量给清除内存了,但有一些时间并不生效了,下面我们就来看看.

<script>ec(2);</script>

配置完成php后,默认php会将session生成到/tmp目录下,导致/tmp目录文件很多,因此需要对session进行定期的清理:

修改php.ini:

[root@hz scripts]# grep "session.save_path = " /usr/local/php/lib/php.ini
;     session.save_path = "N;/path"
;     session.save_path = "N;MODE;/path"
;session.save_path = "/tmp"
session.save_path = "2;/tmp/session"
[root@hz ~]# cat /byrd/script/Cleartmpsen.sh
#!/bin/bash
# Version:1.0
# Author:Byrd
# Site:www.t4x.org
# Contact:root#t4x.org

# This is script will clear php session before 3 hours.
i="0 1 2 3 4 5 6 7 8 9 a b c d e f"
for byrd in $i;
do
    for x in $i;
    do
        mkdir -p /tmp/session/$byrd/$x;
    done;
done
chown -R bywww:bywww /tmp/session
chmod -R 1777 /tmp/session
find /tmp/session -amin +180 -exec rm -rf {} \;
if [ `grep 'session.save_path =' /usr/local/php/lib/php.ini | wc -l` -eq 3 ];then
    sed -i 's#;session.save_path = "/tmp"#;session.save_path = "/tmp"\nsession.save_path = "2;/tmp/session"#g' /usr/local/php/lib/php.ini
else
    exit 1
fi

定时任务:

[root@hz scripts]# echo '#This is a config php session BY:BYRD AT:2014-03-31' >>/var/spool/cron/root
[root@hz scripts]# echo '0 3 * * 0 /bin/bash /byrd/scripts/spehspsion.sh >/dev/null 2>&1' >>/var/spool/cron/root


备注:因为此配置修改了php.ini因此需要重新启动php进程。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template