Php code used to determine the ip blacklist

Source: Internet
Author: User
Tags preg

The ip blacklist is implemented by placing the IP address to be filtered in a txt file or database. Then, when we access the ip address, we can retrieve the IP address and then traverse and compare it.


And supports Class c ip addresses. For example:

Ip.txt
192.168
211.67.188
211.67.191.25
The Code is as follows:

The Code is as follows: Copy code

/*
* IP address blacklist and whitelist
* The ip address of the visitor address is in ip.txt and supports Class c ip addresses.
* By xhat
*/

$ Ip = $ _ SERVER ['remote _ ADDR '];
$ IpArray = preg_replace ("# rn? | N # "," ", file('ip.txt '));
Foreach ($ ipArray as $ ipTest ){
If (substr_count ($ ip, $ ipTest )! = "0 "){
Echo "OK"; // execute related commands
Die ();
}
}

?>

You can use the code above to cheat you. The code below will be upgraded.

The Code is as follows: Copy code

<? Php
Class block_ip {
Var $ Block_ip = array ("192.168.1.1", "210.10.2.1-20", "222.34.4 .*");
Function _ construct (){
}
Function _ destruct (){
}
Private function makePregIP ($ str ){
If (strstr ($ str ,"-")){
$ AIP = explode (".", $ str );
Foreach ($ aIP as $ k =>v v ){
If (! Strstr ($ v ,"-")){
$ Preg_limit. = makePregIP ($ v );
} Else {
$ AipNum = explode ("-", $ v );
For ($ I = $ aipNum [0]; $ I <= $ aipNum [1]; $ I ++ ){
$ Preg. = $ preg? "|". $ I: "[". $ I;
}
$ Preg_limit. = strrpos ($ preg_limit, ".", 1) = (strlen ($ preg_limit)-1 )? $ Preg. "]": ".". $ preg. "]";
}
}
} Else {
$ Preg_limit. = $ str .".";
}
Return $ preg_limit;
}
Private function getAllBlockIP (){
If ($ this-> Block_ip ){
Foreach ($ this-> Block_ip as $ k => $ v ){
$ Ipaddres = $ this-> makePregIP ($ v-> start_ip );
$ Ip = str_ireplace (".", ".", $ ipaddres );
$ Ip = str_replace ("*", "[0-9] {1, 3}", $ ip );
$ Ipaddres = "/". $ ip ."/";
$ Ip_list [] = $ ipaddres;
}
}
Return $ ip_list;
}
Public function checkIP (){
$ Iptable = $ this-> getAllBlockIP ();
$ IsJoined = true;
// Obtain the user ip Address
$ Ip = $ this-> get_client_ip ();
$ Ip = trim ($ Ip );
// Remove the IP segments in the blacklist
If ($ iptable ){
Foreach ($ iptable as $ value ){
If (preg_match ("{$ value}", $ Ip )){
$ IsJoined = false;
Break;
}
}
}
// If www. bKjia. c0m is in the ip blacklist, perform the following operations:
If (! $ IsJoined ){
Echo "IP Error ";
Exit;
}
}
Private function get_client_ip (){
If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown "))
$ Ip = getenv ("HTTP_CLIENT_IP ");
Else if (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown "))
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
Else if (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown "))
$ Ip = getenv ("REMOTE_ADDR ");
Else if (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown "))
$ Ip = $ _ SERVER ['remote _ ADDR '];
Else
$ Ip = "unknown ";
Return ($ ip );
}
}
?>

Reference clip:

The Code is as follows: Copy code

$ OBlock_ip = new block_ip ();
$ OBlock_ip-> checkIP ();

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.