限制每日访问
我想在我的网站中实现一项限制对特定页面的访问的功能。
每个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保存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.
会话和/或 cookie 不是最安全的解决方案:
你的想法看起来并没有那么糟糕;如果你每天只有几百次点击,我想它应该可以处理负载——只需在你的 MySQL 表上使用正确的索引,以帮助你进行选择以检查 IP 是否已经存在。
但请注意,您可以拥有多个使用相同 IP 地址的用户 - 例如,同一家公司的所有人员通常共享相同的 IP。
Sessions and/or cookies are not the most secure solution :
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.