PHP - 简单的点击计数器

发布于 2024-12-05 20:12:57 字数 190 浏览 3 评论 0原文

为多个页面创建点击计数器并同时优化性能的最佳方法是什么?例如,我希望在每个不同的博客文章以及主页上都有一个独特的点击计数器。为了使其唯一,我必须将用户 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 技术交流群。

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

发布评论

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

评论(1

独留℉清风醉 2024-12-12 20:12:57

你可能会因为你的问题而受到盘问,但我说如果你想把事情做好,你就必须自己做。正确的?

以我的愚见,我会让它向数据库提交 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".

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