PHP restricts IP access. only access from specified IP addresses is allowed. wildcard * is allowed to filter IP addresses.

Source: Internet
Author: User
[Php] 01. /** 02. * check whether the Accessed ip address is specified to allow ip03. * Enterdescriptionhere... 04. */05. functioncheck_ip () {06. $ ALLOWED_IParray (& amp; #39; 192.168.2. * & amp; #39;, & amp; #39; 127.

[Php]

01 ./**
02. * check whether the Accessed ip address is a specified allowed ip address.
03. * Enter description here...
04 .*/
05. function check_ip (){
06. $ ALLOWED_IP = array ('192. 192. 2. * ', '192. 0.0.1', '192. 168.2.49 ');
07. $ IP = getIP ();
08. $ check_ip_arr = explode ('.', $ IP); // split the ip address to be detected into an array
09. # restrict IP addresses
10. if (! In_array ($ IP, $ ALLOWED_IP )){
11. foreach ($ ALLOWED_IP as $ val ){
12. if (strpos ($ val ,'*')! = False) {// A * replacement character is found
13. $ arr = array ();//
14. $ arr = explode ('.', $ val );
15. $ bl = true; // used to record whether matching is successful in loop detection
16. for ($ I = 0; $ I <4; $ I ++ ){
17. if ($ arr [$ I]! = '*') {// It is not equal to * and will be checked in. if it is a * symbol replacement, it will not be checked.
18. if ($ arr [$ I]! = $ Check_ip_arr [$ I]) {
19. $ bl = false;
20. break; // terminate the check to continue to check the next ip address
21 .}
22 .}
23.} // end
24. if ($ bl) {// if it is true, if a match is found, return
25. return;
26. die;
27 .}
28 .}
29.} // end foreach
30. header ('http/1.1 403 Forbidden ');
31. echo "Access forbidden ";
32. die;
33 .}
34 .}
/**
* Check whether the Accessed ip address is a specified allowed ip address.
* Enter description here...
*/
Function check_ip (){
$ ALLOWED_IP = array ('192. 192. 2. * ', '192. 0.0.1', '192. 168.2.49 ');
$ IP = getIP ();
$ Check_ip_arr = explode ('.', $ IP); // split the ip address to be detected into an array.
# IP address restriction
If (! In_array ($ IP, $ ALLOWED_IP )){
Foreach ($ ALLOWED_IP as $ val ){
If (strpos ($ val ,'*')! = False) {// A * replacement character is found
$ Arr = array ();//
$ Arr = explode ('.', $ val );
$ Bl = true; // used to record whether matching is successful in loop detection
For ($ I = 0; $ I <4; $ I ++ ){
If ($ arr [$ I]! = '*') {// It is not equal to * and will be checked in. if it is a * symbol replacement, it will not be checked.
If ($ arr [$ I]! = $ Check_ip_arr [$ I]) {
$ Bl = false;
Break; // terminate the check. this ip address continues to check the next ip address.
}
}
} // End
If ($ bl) {// if it is true, if it is found that a match is successful, return
Return;
Die;
}
}
} // End foreach
Header ('http/1.1 403 Forbidden ');
Echo "Access forbidden ";
Die;
}
} [Php]
01.
[Php]
01.

/**
02. * obtain the Accessed IP address
03. * Enter description here...
04 .*/
05. function getIP (){
06. return isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]
07. :( isset ($ _ SERVER ["HTTP_CLIENT_IP"])? $ _ SERVER ["HTTP_CLIENT_IP"]
08.: $ _ SERVER ["REMOTE_ADDR"]);
09 .}

 
10.
  

11.
 
12. add check_ip (); to be checked. this function only allows the specified IP address to access the file, and provides wildcard * in the IP address to match multiple IP addresses www.2cto.com.
[Php]
/*** Obtain the Accessed IP ** Enter description here... */function getIP () {return isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"] :( isset ($ _ SERVER ["HTTP_CLIENT_IP"])? $ _ SERVER ["HTTP_CLIENT_IP"]: $ _ SERVER ["REMOTE_ADDR"]);}/**
* Obtain the Accessed IP address
* Enter description here...
*/
Function getIP (){
Return isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]
:( Isset ($ _ SERVER ["HTTP_CLIENT_IP"])? $ _ SERVER ["HTTP_CLIENT_IP"]
: $ _ SERVER ["REMOTE_ADDR"]);
}


Add the check_ip (); to be detected. This function allows only the specified IP address to access the file, and provides the * wildcard in the IP address to match multiple IP addresses.

 

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.