禁用IP的mysql php設計

2021-04-13 07:32:04 字數 1170 閱讀 3184

禁用ip是安全的一部份。下面就分析下禁止ip的php+mysql的程式設計,不足之處,請大家提出意見。

就來個簡單的例子來說明一下。

create table `banned` (

`ip_start` int(10) not null default '0',

`ip_end` int(10) not null default '0',

) type=myisam comment='禁止ip列表';

上面是新建乙個資料表,表有兩個字段用來記錄要禁用ip的起始。ip_tart是開始,ip_end是結束。ip_start的值要比ip_end值小或相等。範圍 是0~4294967295。如果只有乙個ip,那麼這條記錄ip_start與ip_end的字段值相同。

注意,這兩個欄位都是以int型別出現的,為什麼呢?因為乙個ip是可以轉換為乙個相對應的十進位制數字。數字大小的對比占有很強的優勢。

下例輸入乙個禁止的ip。127.0.0.1

insert into `banned` (`ip_start`, `ip_end`) values (inet_aton( '127.0.0.1' ),inet_aton( '127.0.0.1' ));

如果要禁用一段ip,如192.168.1.1~192.168.255.2255

insert into `banned` (`ip_start`, `ip_end`) values (inet_aton( '192.168.1.1' ),inet_aton( '192.168.255.255' ));

我們可以從資料表中看到,上面插入的記錄的 ip_start與ip_end字段值是2130706433。因為mysql的函式inet_aton將ip轉為了數字形式。

現在,禁止乙個ip,只要看一下,這個ip是不是在這個資料表裡所包函的一條記錄。

$ip = '127.0.0.1';

$sql = "select count(*)         from `banned` where ip_start  >=inet_aton('$ip') and ip_end <= inet_aton('$ip') ";

$result =  mysql_query($sql);

$banned = mysql_fetch_array($result);

echo $banned[0] ? "禁止ip" : "通行ip" ; 

nginx封ip,禁用IP段的設定說明

nginx的ngx http access module 模組可以封配置內的ip或者ip段,語法如下 deny ip deny subnet allow ip allow subnet block all ips deny all allow all ips allow all 如果規則之間有衝突,...

如何通過WEB方式,來控制iis的禁用IP名單

如何通過web方式,來控制iis的禁用ip名單。這個問題可以進一步劃分為兩個問題 1 如何控制iis的ipdeny 2 由於是web方式,預設的web帳戶許可權很低,不會有上面操作的許可權,如何處理。第乙個問題 微軟的msdn中給出了三種方法 這裡給出了兩種。其實都是使用 system.direct...

RHEL6徹底禁用ip6的方法

rhel6徹底禁用ip6的方法 一 vi etc modprobe.d disable ipv6.conf 名字隨便起 rhel6.0之後沒有了 etc modprobe.conf這個檔案 輸入 install ipv6 bin true 二 vi etc sysconfig network 在最後...