Php code used to determine the ip blacklist

Source: Internet
Author: User
This file can be added and supports Class c ip addresses, for example, ip.txt... hosts, and Class c ip addresses, for example:

ip.txt 192.168 211.67.188 211.67.191.25

The PHP instance code is as follows:

 

The above code can be used to cheat you, and the following code will be upgraded:

 $ 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;} p Ublic function checkIP () {$ iptable = $ this-> getAllBlockIP (); $ IsJoined = true; // obtain the user ip address $ Ip = $ this-> get_client_ip (); $ Ip = trim ($ Ip); // remove the IP segment in the blacklist if ($ iptable) {foreach ($ iptable as $ value) {if (preg_match ("{$ value}", $ Ip) {$ IsJoined = false; break ;}}} // 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 the snippet. the code is as follows:

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


Tutorial link:

Reprint at will ~ However, please keep the tutorial address★

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.