PHP restricts IP access and only allows access from specified IP addresses

Source: Internet
Author: User
PHP restricts IP access and only allows access from specified IP addresses

  1. /**

  2. * Check whether the Accessed ip address is a specified allowed ip address.
  3. * Enter description here...
  4. */
  5. Function check_ip (){
  6. $ ALLOWED_IP = array ('192. 192. 2. * ', '192. 0.0.1', '192. 168.2.49 ');
  7. $ IP = getIP ();
  8. $ Check_ip_arr = explode ('.', $ IP); // split the ip address to be detected into an array.
  9. # IP address restriction
  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. this ip address continues to check the next ip address.
  21. }
  22. }
  23. } // End
  24. If ($ bl) {// if it is true, if it is found that a match is successful, 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. }

  35. /**

  36. * Obtain the Accessed IP address
  37. * Enter description here...
  38. */
  39. Function getIP (){
  40. Return isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]
  41. :( Isset ($ _ SERVER ["HTTP_CLIENT_IP"])? $ _ SERVER ["HTTP_CLIENT_IP"]
  42. : $ _ SERVER ["REMOTE_ADDR"]);
  43. }

Call Method: Add check_ip (); to the required file to achieve the IP address access restriction. This function allows only the specified IP address to access the file, the wildcard character * in the IP address matches 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.