Home  >  Article  >  Backend Development  >  How to count website visitors in php

How to count website visitors in php

(*-*)浩
(*-*)浩Original
2019-09-29 13:46:546509browse

A simple method for php to count the number of website visits

How to count website visitors in php

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[&#39;#&#39;])  
    {  
     $_SESSION[&#39;#&#39;] = 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!

Statement:
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