Home > php教程 > php手册 > body text

File records web page visits

WBOY
Release: 2016-09-06 08:42:43
Original
1873 people have browsed it

The file records the number of web page visits and is normal when used under high concurrency;
$file = dirname(__FILE__).'/tongji.db';
//$data = unserialize(file_get_contents($file));
$fp=fopen($file,'r+');
$content='';

if (flock($fp,LOCK_EX)){
while (($buffer=fgets($fp,1024))!=false){
$content=$content.$buffer;
}
$data=unserialize($content);

//Set record key value
$total = 'total';
$month = date('Ym');
$today = date('Ymd');
$yesterday = date('Ymd',strtotime("-1 day"));
$tongji = array();
//Total visits increased
$tongji[$total] = $data[$total] + 1;
// Visits increased this month
$tongji[$month] = $data[$month] + 1;
// Increased visits today
$tongji[$today] = $data[$today] + 1;
//Keep visiting yesterday
$tongji[$yesterday] = $data[$yesterday];

//Save statistics
ftruncate($fp,0); // Truncate the file to the given length
rewind($fp); // Rewind the position of the file pointer
fwrite($fp, serialize($tongji));
flock($fp,LOCK_UN);
fclose($fp);

//Output data
$total = $tongji[$total];
$month = $tongji[$month];
$today = $tongji[$today];
$yesterday = $tongji[$yesterday]?$tongji[$yesterday]:0;
echo "document.write('President visited {$total}, this month {$month}, yesterday {$yesterday}, today {$today}');";

}

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