如何像stackoverflow一样获取页面浏览数据?
我想获取并显示一个页面被查看的次数,就像 stackoverflow 一样。
怎么用php来做呢?谢谢!
I want to get and display how many times a page is viewed, just like stackoverflow.
How to do it by php? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于任何“体面”的计数器,我建议使用数据库(mysql、redis)和跟踪 IP 地址来进行更深入的分析(例如,有多少唯一访问量、它们来自何处等)
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)
您需要将该信息存储在某个地方,这实际上不能仅使用 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.