Php code for restricting access from a single IP address and ip address segment

Source: Internet
Author: User
Php code for restricting access from a single IP address and ip address segment
I will share two pieces of php code that limits IP address access. There are two examples of code: restrict access from a single IP address and restrict access from an IP address segment. For more information, see.

  1. // Add IP address access restrictions
  2. If (getenv ('http _ CLIENT_IP ') & strcasecmp (getenv ('http _ CLIENT_IP'), 'Unknown ')){
  3. $ Userip = getenv ('http _ CLIENT_IP ');
  4. } Elseif (getenv ('http _ X_FORWARDED_FOR ') & strcasecmp (getenv ('http _ X_FORWARDED_FOR'), 'Unknown ')){
  5. $ Userip = getenv ('http _ X_FORWARDED_FOR ');
  6. } Elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR'), 'Unknown ')){
  7. $ Userip = getenv ('remote _ ADDR ');
  8. } Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], 'Unknown ')){
  9. $ Userip = $ _ SERVER ['remote _ ADDR '];
  10. }
  11. $ Banned_ip = array (
  12. "127.0.0.1 ",
  13. "203.0.0.1 ",
  14. "56.12.50.65 ",
  15. "192.168.1.88"
  16. );
  17. If (in_array ($ userip, $ banned_ip ))
  18. {
  19. Die ("Your IP is block to connect! ");
  20. }
  21. Echo "test code! Test ";
  22. ?>

Example 2: IP segment restriction: restrict IP segment access.

  1. // Add IP address access restrictions
  2. If (getenv ('http _ CLIENT_IP ') & strcasecmp (getenv ('http _ CLIENT_IP'), 'Unknown ')){
  3. $ Userip = getenv ('http _ CLIENT_IP ');
  4. } Elseif (getenv ('http _ X_FORWARDED_FOR ') & strcasecmp (getenv ('http _ X_FORWARDED_FOR'), 'Unknown ')){
  5. $ Userip = getenv ('http _ X_FORWARDED_FOR ');
  6. } Elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR'), 'Unknown ')){
  7. $ Userip = getenv ('remote _ ADDR ');
  8. } Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], 'Unknown ')){
  9. $ Userip = $ _ SERVER ['remote _ ADDR '];
  10. }
  11. $ Ban_range_low = ip2long ("217.0.0.0"); // on the ip address segment
  12. $ Ban_range_up = ip2long ("217.00000000255"); // ip end
  13. $ Ip = ip2long ($ userip]);
  14. If ($ ip> $ ban_range_low & $ ip <$ ban_range_up)
  15. {
  16. Print "Banned ";
  17. Exit ();
  18. }
  19. ?>



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.