Home > Backend Development > PHP Tutorial > How to automatically clean the session in php regularly, phpsession cleaning_PHP tutorial

How to automatically clean the session in php regularly, phpsession cleaning_PHP tutorial

WBOY
Release: 2016-07-12 09:05:04
Original
798 people have browsed it

How to clean the session regularly and automatically in php, phpsession cleaning

The following will introduce to you an example of setting up the session to automatically clean up regularly in PHP, because the session defaults to automatically clearing variables every 15 minutes. The memory was cleared, but it did not take effect for some time. Let’s take a look below.

After configuring php, by default php will generate the session into the /tmp directory, resulting in a large number of files in the /tmp directory, so the session needs to be cleaned regularly.

Modify 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:2015-11-12' >>/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 

Copy after login

Note: Because this configuration modifies php.ini, the php process needs to be restarted.

The above is the entire content of this article. It teaches you how to implement regular automatic cleaning of PHP sessions. I hope you can gain something from it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1071228.htmlTechArticleHow to automatically clean sessions regularly in php. phpsession Cleanup The following will introduce you to an example of setting up PHP sessions to automatically clean up regularly. Yes, because the session automatically clears variables every 15 minutes by default...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template