Php IP address range restriction method

Source: Internet
Author: User
This article mainly introduces php's method of limiting IP address ranges, which involves php's IP address operation skills and is very useful, for more information about how to limit the IP address range of php, see the example in this article. Share it with you for your reference. The details are as follows:

Only IP addresses within the specified range can access

function get_real_ipaddress() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) {  return $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {  return $_SERVER['HTTP_X_FORWARDED_FOR']; } return $_SERVER['REMOTE_ADDR'];}function in_ip_range($ip, $ip_one, $ip_two = false) { if(!$ip_two) {  return $ip_one === $ip; } return ip2long($ip_one) * -1 >= ip2long($ip) * -1 && ip2long($ip_two) * -1 <= ip2long($ip) * -1;}function validate_ip() { $ip = explode(':', get_real_ipaddress()); $ip = $ip[0]; if(in_ip_range($ip, '212.76.229.115', '212.76.229.120')) {  return true; } else if(in_ip_range($ip, '194.78.4.66', '194.78.4.79')) {  return true; } else if(in_ip_range($ip, '194.8.4.78', '194.8.4.78')) {  return true; } else if(in_ip_range($ip, '0', '1')) { // local  return true; } header('Location: http://www.bitsCN.com');}

I hope this article will help you with php programming.

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.