简体   繁体   English

使用PHP(Laravel)和MySQL禁止IP,IP范围,DNS或Agend

[英]Ban IP, IP range, DNS or Agend with PHP (Laravel) and MySQL

I want to implement a ban system. 我想实施禁令制度。 Firstly, I will create a filter that will make sure banned users are redirected to a page informing them about the ban. 首先,我将创建一个过滤器,以确保将被禁止的用户重定向到通知他们该禁止的页面。 I use Laravel and MySQL as database. 我使用Laravel和MySQL作为数据库。 My table could look like this: 我的桌子看起来像这样:

id, ip , range, dns, agent, reason, endofban

or like this 或像这样

id, rule, reason, endofban

where reason could be a JSON string like this: reason可能是这样的JSON字符串:

{ "ip": "123.22.12.*" }
{ "dns": "malicious.dns.com" }
{ "range": "123.22.12.0 - 123.22.14.0" }

The advantage of the last one is being more compact but I don't know if there's a fast way to check that JSON against the information of the visitor. 最后一个的优点是更紧凑,但是我不知道是否有一种快速方法可以根据访问者的信息检查JSON。 I don't want to get all of it from the database then do a for loop to make sure no rule returns positive. 我不想从数据库中获取所有内容,然后执行for循环以确保没有规则返回正数。 I want to make the check in the database. 我想在数据库中进行检查。 This must be done every-time a page is loaded, so it must be fast. 每次加载页面时都必须执行此操作,因此必须快速。 What do you think? 你怎么看? Any other solution? 还有其他解决方案吗? Also, how would I check an IP range or wildcard in MySQL? 另外,如何在MySQL中检查IP范围或通配符?

ip is redundant with range . iprange冗余。 A single IP is simply a range where the start and end are the same value. 单个IP只是起点和终点为相同值的范围。

I would create two values, ip_range_start and ip_range_end . 我将创建两个值ip_range_startip_range_end Instead of storing strings, store the IP address as an integer (ie, the result of INET_ATON() ). 代替存储字符串,将IP地址存储为整数(即INET_ATON()的结果)。 Then, when you run your query you can say: 然后,当您运行查询时,您可以说:

SELECT *
FROM AccessDenyList
WHERE INET_ATON('192.0.2.115') BETWEEN ip_start_range AND ip_end_range

Normalise your datastructure. 规范化您的数据结构。 The JSON is part of your (relational?) data, so save an IP as an IP, a range as a range, and query against that. JSON是您(关系型?)数据的一部分,因此将IP保存为IP,将范围保存为范围,然后查询该范围。

So make datatypes you can check against (in your database). 因此,使数据类型可以检查(在数据库中)。 This does mean you have to check with an OR for all these types (ip = xxxx OR dns = yyy or ip-inragne(zzz), but that's a given :) 这确实意味着您必须针对所有这些类型使用OR进行检查(ip = xxxx OR dns = yyy或ip-inragne(zzz),但这是给定的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM