如何使用基于PHP/MySQL的脚本禁止网站中的IP块? [英] How do I use a PHP/MySQL-based script to ban IP blocks from a website?
问题描述
我要禁止的IP和IP阻止列表很大.我的列表是按块完成的,当前的格式为:
I have a fairly large list of IPs and IP blocks that I'd like to ban. The list I have is done in blocks, currently in such a format:
1.2.3.4-1.2.3.54
5.6.7.8-5.6.7.8
2.3.4.5-2.3.4.116
我试图通过某种方式将它们放入MySQL数据库中,以找到最有效的方法来阻止这些IP.当然,我可以将所有块分成单独的IP,并将它们放在自己的MySQL记录中,但是对于大量IP,这将是极其低效的.阻止整个范围(例如"1.2.3.*")也会导致不必要地禁止大量用户.
I'm trying to find the most efficient way to block these IPs by somehow putting them into a MySQL database. I could, of course, separate all the blocks into separate IPs and put them in their own MySQL records, but that would be extremely inefficient with a large number of IPs. Blocking entire ranges, such as "1.2.3.*" would also result in unnecessarily banning a lot users.
有没有办法有效地做到这一点?
Is there a way to do this effectively?
推荐答案
您可以使用MySQL INET_ATON()和INET_NTOA()函数.请记住,这些是特定于MySQL的,但是您也可以使用php来实现它们.
You could use the MySQL INET_ATON() and INET_NTOA() functions. Keep in mind these are MySQL specific, but you can implement them using php as well.
这些函数将IP地址从点表示法转换为整数.然后,您可以将它们存储在带有"start_ip"和"end_ip"列的表中.在查询以查看是否禁止IP时,可以对BETWEEN使用查询.
These functions convert an IP address from dot notation in to an integer. You can then store them in a table with "start_ip" and "end_ip" columns. When you are querying to see if an IP is banned, you can use a query with BETWEEN.
SELECT * FROM bans WHERE INET_ATON("127.0.0.1") BETWEEN start_ip AND end_ip
这篇关于如何使用基于PHP/MySQL的脚本禁止网站中的IP块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!