PHP - 简单的点击计数器
为多个页面创建点击计数器并同时优化性能的最佳方法是什么?例如,我希望在每个不同的博客文章以及主页上都有一个独特的点击计数器。为了使其唯一,我必须将用户 IP 地址存储在数据库或文本文件中。我可以将它存储在cookie中吗?我对cookie不太熟悉,这句话可能根本没有意义。
无论如何,在 PHP 中为不同页面创建单独的唯一视图计数器的最佳方法是什么?
What is the best way to create a hit counter for multiple pages, and to optimize performance at the same time. For example, I would like a unique hit counter on each different blog post, as well as the main page. To make it unique, I would have to store the users IP address in the database, or in a text file. Could I store it in a cookie? I'm not too familiar with cookies, and this sentence might make no sense at all.
Anyway, what is the best way to create separate unique view counters for different pages in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可能会因为你的问题而受到盘问,但我说如果你想把事情做好,你就必须自己做。正确的?
以我的愚见,我会让它向数据库提交 2 个值。 (1) “唯一”访问者,仅当用户没有该 cookie 时才会被存储,以及 (2) “页面浏览”访问者,无论其 cookie 的状态如何,都会被存储。确保它是一个“int”类型字段,这样它就不会像浮点数、双精度数或文本那样使用那么多内存。
最后,我想说你应该优化未编写的软件。这就是在实现代码之前进行规划的重点。有句老话说,“测量两次,切割一次”。
You may be getting grilled for your question but I say if you want it done right, you gotta do it yourself. Right?
In my humble opinion, I would have it submit 2 values to a database. (1) the "unique" visitors, which would only be stored when the user DOESN'T have that cookie, and (2) the "page view" visitors, which would be stored regardless of the status of their cookie. Make sure it's an "int"-type field so that it doesn't use as much memory as say a float, double, or text.
Lastly, I would say that you SHOULD optimize unwritten software. That's the point of planning it out before you implement code. There's an old saying, "Measure twice, cut once".