A simple method for php to count the number of website visits
Here mainly uses session to save the current visitor and store the number of visits Write to local file (recommended learning: PHP video tutorial)
<? @session_start(); $counter = intval(file_get_contents("counter.dat")); //创建一个dat数据文件 if(!$_SESSION['#']) { $_SESSION['#'] = true; $counter++; //刷新一次+1 $fp = fopen("counter.dat","w"); //以写入的方式,打开文件,并赋值给变量fp fwrite($fp, $counter); //将变量fp的值+1 fclose($fp); } ?>
Enter code
<?php echo "$counter";?>
Case display
您是到访的第<?php echo "$counter";?>位用户
The above is the detailed content of How to count website visitors in php. For more information, please follow other related articles on the PHP Chinese website!