限制每日访问

发布于 2024-10-24 15:19:26 字数 479 浏览 8 评论 0原文

我想在我的网站中实现一项限制对特定页面的访问的功能。

每个 IP 每天应允许 2 次访问。

我正在考虑创建一个 mysql 表并写入 ip + 访问该网站的 ip 时间,然后创建一个每 24 小时删除所有条目的 cron 作业。

但我担心他可能会导致服务器负载过多(以防我每天有几百次访问)。有没有更好或更简单的方法来限制每个IP每天访问两次?

我也在考虑使用 cookie 或会话,但关于如何创建 cookie 来完成我需要的工作,我有 0 个计划......

我很感谢任何建议!

编辑:

在有用的评论之后,我还需要两件事的建议:

- 公共代理检测脚本是否足以阻止至少匿名和网络代理?

- 是否可以单独限制每个 IP 的访问并创建一个脚本,该脚本将在第一次访问后 24 小时内删除该 IP 的条目?而不是让 cron 作业每 24 小时删除一次所有数据,无论访问者第一次访问我的网站的时间如何?

I would like to implent a feature in my website that limits the access to a certain page.

It should allow 2 access per day per IP.

I was thinking of creating a mysql table and write in the ip+times the ip visited the website and than create a cron job that delets all entries every 24 hours.

But I am worried that his may causes too much server load (in case I get a few hundreth visits a day). Is there a better or simpler method to limit the daily access to two times per IP?

I was also thinking of using cookies or sessions, but I have got 0 plans on how to create a cookie that would do the job I need....

I am thankfull for any suggestions!

EDIT:

Two more things that I would need an advice about, after the helpful comments:

-will a public proxy detection script be enough to keep out at least the anonymous and web proxies?

-is it possible to limit the access for each IP individually and create a script, that will remove the entries for the IP exactly 24 hours after the first visit? Instead of having the cron job delete all data every 24 hours regardless of the time a visitor first came to my site?

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

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

发布评论

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

评论(2

深海夜未眠 2024-10-31 15:19:26

保存ip、时间和访问数据库的次数就可以了,几百行对于mysql数据库来说不是问题(不要忘记在表上设置索引)。

但请注意,具有共享 IP 的用户可能无法访问您的页面。

使用 cookie 是有问题的,因为用户可以控制它,而你无法限制它。当然,这取决于您的应用程序阻止用户 aftexn 访问的重要性。

Saving ip, time and number of accesses to the database is fine, a few hundred rows is not a problem for a mysql database (don't forget to set indexes on the table).

But note that users with shared IP might have problems accessing your page.

Using cookies is problematic because the user can control this and you can't restrict that. Of course, it depends on your application how important is to block the user aftex n accesses.

北方的韩爷 2024-10-31 15:19:26

会话和/或 cookie 不是最安全的解决方案:

  • 会话在一段时间后过期(通常是半小时,默认设置)
  • 用户可以删除 Cookie;或者他可以更换浏览器

你的想法看起来并没有那么糟糕;如果你每天只有几百次点击,我想它应该可以处理负载——只需在你的 MySQL 表上使用正确的索引,以帮助你进行选择以检查 IP 是否已经存在。

但请注意,您可以拥有多个使用相同 IP 地址的用户 - 例如,同一家公司的所有人员通常共享相同的 IP。

Sessions and/or cookies are not the most secure solution :

  • Sessions expire after a while (typically something like half an hour, with default settings)
  • Cookies can be deleted by the user ; or he can change browser

Your idea doesn't seem that bad ; and if you only have a couple hundred hits a day, it should handle the load, I suppose -- just use the right indexes on your MySQL table, to help with the selects you'll do to check if an IP has already been there.

But note that you can have more than one user, using the same IP address -- for instance, all people in the same company often share the same IP.

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