Home > Database > Mysql Tutorial > body text

How to limit the IP address range in PHP

怪我咯
Release: 2017-07-11 16:41:02
Original
1613 people have browsed it

This article mainly introduces the method of limiting the IP address range in PHP. It involves the skills of operating IP addresses in PHP. It is of great practical value. Friends in need can refer to it.

The example in this article tells about the PHP limitation of IP address. range method. Share it with everyone for your reference. The details are as follows:

Only IP addresses within a limited 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(&#39;:&#39;, get_real_ipaddress());
 $ip = $ip[0];
 if(in_ip_range($ip, &#39;212.76.229.115&#39;, &#39;212.76.229.120&#39;)) {
  return true;
 } else if(in_ip_range($ip, &#39;194.78.4.66&#39;, &#39;194.78.4.79&#39;)) {
  return true;
 } else if(in_ip_range($ip, &#39;194.8.4.78&#39;, &#39;194.8.4.78&#39;)) {
  return true;
 } else if(in_ip_range($ip, &#39;0&#39;, &#39;1&#39;)) { // local
  return true;
 }
 header(&#39;Location: http://www.jb51.net&#39;);
}
Copy after login

The above is the detailed content of How to limit the IP address range in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template