Wildcard IP Banning with MySQL

前端 未结 5 1883
甜味超标
甜味超标 2021-02-01 11:01

I\'m trying to implement an IP banning system into my web app using MySQL, i know i can do it using .htaccess but that\'s not neat to me.

Basically my curre

5条回答
  •  面向向阳花
    2021-02-01 11:42

    convert wildcards from 42.21.*.* to 42.21.0.0 and back when writing or reading entries from database. For efficiency (low memory and disk footprint, performance) store it as an integer, use INET_NTOA and INET_ATON for conversion.

    when you look up an IP address a.b.c.d:

    SELECT ip FROM ip_blacklist WHERE ip=INET_ATON('a.b.c.d') or ip=INET_ATON('a.b.c.0') or ip=INET_ATON('a.b.0.0') or ip=INET_ATON('a.0.0.0')
    

    Ok, the last match is probably silly.

    Don't forget to add indexes.

提交回复
热议问题