如何像stackoverflow一样获取页面浏览数据?

发布于 2024-11-27 11:25:37 字数 69 浏览 1 评论 0原文

我想获取并显示一个页面被查看的次数,就像 stackoverflow 一样。

怎么用ph​​p来做呢?谢谢!

I want to get and display how many times a page is viewed, just like stackoverflow.

How to do it by php? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2024-12-04 11:25:37
if (file_exists('count_file.txt')) 
    {
    $fil = fopen('count_file.txt', r);
    $dat = fread($fil, filesize('count_file.txt')); 
    echo $dat+1;
    fclose($fil);
    $fil = fopen('count_file.txt', w);
    fwrite($fil, $dat+1);
    }

    else
    {
    $fil = fopen('count_file.txt', w);
    fwrite($fil, 1);
    echo '1';
fclose($fil);
}
?>

对于任何“体面”的计数器,我建议使用数据库(mysql、redis)和跟踪 IP 地址来进行更深入的分析(例如,有多少唯一访问量、它们来自何处等)

if (file_exists('count_file.txt')) 
    {
    $fil = fopen('count_file.txt', r);
    $dat = fread($fil, filesize('count_file.txt')); 
    echo $dat+1;
    fclose($fil);
    $fil = fopen('count_file.txt', w);
    fwrite($fil, $dat+1);
    }

    else
    {
    $fil = fopen('count_file.txt', w);
    fwrite($fil, 1);
    echo '1';
fclose($fil);
}
?>

For any "decent" counter I would recommend to use a database (mysql, redis ) and trace IP address to have even deeper analytics (e.g how many unique visits, where they are comming from etc)

浅唱々樱花落 2024-12-04 11:25:37

您需要将该信息存储在某个地方,这实际上不能仅使用 PHP 来完成。

最常见的是,它存储在数据库中。最简单的解决方案是每页一个数据库行,您希望使用一列来跟踪该行以记住视图计数。每次加载页面时,您都会增加此列。

稍微复杂一点,但更有用的是,在每个页面加载时添加一个数据库行,记录页面、时间和您可能认为有用的任何其他信息。

捕获此信息的另一种简单方法是在您的站点上安装分析包。类似于 Google Analytics,它是免费的。但是,它并不是专门为显示页面视图而设计的,只是捕获它们。

You will need to store that information somewhere, which isn't really something that you can do with only PHP.

Most commonly, this is stored in a database. The simplest solution is a single database row per page that you wish to track with a column for remembering the view count. You would increment this column each time the page is loaded.

Slightly more complicated, but far more useful, would be to add a database row on each page load, noting the page, time, and any other information that you might find useful.

Another simple way to capture this information would be to install an analytics package on your site. Something like Google Analytics, which is free. But, it isn't particularly tailored to displaying page views, simply capturing them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文