PHP only allows you to specify IP access (allow * Wildcard filter IP) _php instance

Source: Internet
Author: User
Tags explode ip number

The

Core function code is as follows:

/** * Detect Access IP is the specified allowed IP * Enter description here ... */function check_ip () {$ALLOWED _ip=array (' 192.168.2.* ', ' 127.0.
	0.1 ', ' 192.168.2.49 ');
	$IP =getip ();
		  $check _ip_arr= explode ('. ', $IP)//IP split into array #限制IP if (!in_array ($IP, $ALLOWED _ip)) {foreach ($ALLOWED _ip as $val) {
		  	 if (Strpos ($val, ' * ')!==false) {//found to have an asterisk substitution character $arr =array ();//$arr =explode ('. ', $val); $BL =true;//is used to record whether there is a match in the Loop detection for ($i =0 $i <4; $i + +) {if ($arr [$i]!= ' * ') {//Not equal to * will come in to detect if the * symbol substitution is not checked if ($arr [$i]!= $check _ip_arr[$i])
		  	 			{$BL =false;
		  	 	break;//terminate check this IP to continue checking the next IP}}//end for if ($BL) {//If it is true to find a match that succeeds returns return;
		  	 Die
		}}//end foreach header (' http/1.1 403 Forbidden ');
		echo "Access forbidden";
	Die * Access IP * Enter description here ... */function GetIP () {return isset ($_server["http_x_forwarded_for"])? $_se rver["Http_x_forwarded_for"]:(isset ($_server["http_client_ip"])? $_server["Http_Client_ip "]: $_server[" REMOTE_ADDR "]); }

Add the call to Check_ip () where it needs to be detected, and;  this function to provide only the specified IP Access file, and to provide a wildcard matching ip  in the IP number;

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.