Php code for limiting IP addresses and IP segments

Source: Internet
Author: User
Tags preg
The php restricted IP segment code was found when I searched for related solutions on the internet. the logic of the makePregIP function of this class is a bit problematic. I modified it and can use it, this function allows access from IP addresses in the whitelist. if you want to restrict IP addresses in the blacklist... the php restricted IP segment code was found when I searched for related solutions on the internet. the logic of the makePregIP function of this class is a bit problematic. I modified it and can use it, this function allows access from IP addresses in the whitelist. to restrict access from IP addresses in the blacklist, simply modify the code logic in the checkIP function.

The code is as follows:

 CheckIP () {echo 'Your IP address is: '; echo $ oBlock_ip-> ip address; exit ('Access prohibited') ;}?>

The allowIP file code is as follows:

 Allow_ip = $ allow_ip; $ this-> ip = '';} private function makePregIP ($ str) {if (strstr ($ str ,"-")) {$ aIP = explode (". ", $ str); foreach ($ aIP as $ k => $ v) {if (! Strstr ($ v, "-") {$ preg_limit. = $ this-> makePregIP ($ v); $ preg_limit. = ". ";}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-> allow_ip) {$ I = 1; foreach ($ this-> allow_ip as $ k => $ v) {$ ipaddres = $ this-> makePregIP ($ v ); $ ip = str_ireplace (". ",". ", $ ipaddres); $ ip = str_replace (" * "," [0-9] {1, 3} ", $ ip); $ ipaddres = "/". $ ip. "/"; $ ip_list [] = $ ipaddres; $ I ++ ;}}return $ ip_list;} pub Lic function checkIP () {$ iptable = $ this-> getAllBlockIP (); $ IsJoined = false; // obtain the user ip address $ Ip = $ this-> get_client_ip (); $ Ip = trim ($ Ip); // in the whitelist, if ($ iptable) {foreach ($ iptable as $ value) {if (preg_match ("{$ value}", $ Ip) {$ IsJoined = true; break ;}}// if (! $ IsJoined) {return false;} return true;} private function get_client_ip () {if (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP "), "unknown") $ ip = getenv ("HTTP_CLIENT_IP"); else if (getenv ("condition") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown ")) $ ip = getenv ("HTTP_X_FORWARDED_FOR"); else if (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "u Nknown ") $ 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"; $ this-> ip = $ ip; return ($ ip) ;}}?>


Tutorial address:

Reprinted! But please include the article 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.