Home > Backend Development > PHP Problem > How to count website visitors in php

How to count website visitors in php

(*-*)浩
Release: 2023-02-25 15:38:02
Original
6519 people have browsed it

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);  
    }  
?>
Copy after login

Enter code

<?php echo "$counter";?>
Copy after login

Case display


您是到访的第<?php echo "$counter";?>位用户

Copy after login

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!

Related labels:
php
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