Home > Backend Development > PHP Tutorial > Determine whether an IP address exists within the range in php

Determine whether an IP address exists within the range in php

不言
Release: 2023-03-23 16:40:01
Original
3258 people have browsed it

The content of this article is to share with you about the method of determining whether an IP address exists within the range in PHP. Friends in need can refer to it

I have seen many kinds of IP addresses on the Internet. Range methods include regular ones and string interception. I have tried a lot and feel that it is not very reliable. Firstly, it is a bit reluctant to give up on the accuracy. Secondly, it hurts the performance too much and is complicated. After searching a lot of information, I found a simple method to determine the IP address range with better performance. I will make a note here.

As for the IP address database, it is really good: http://www.cz88.net/

You can also use the online interface, but there are problems. You cannot use for loops to access frequently, otherwise it will be blocked by the interface

Taobao IP address database: http://ip.taobao.com/service/getIpInfo.php?ip=Fill in IP here Address


Okay, in fact, it is very simple to actually determine the IP address range. It can be done with one line of code. You will understand after looking at the case I wrote below:





##[php] view plain copy

//案例:判断192.168.1.127是否在 (192.168.1.1--192.168.1.255)的范围里面  
  
$ip_start = get_iplong('192.168.1.1'); //起始ip  
$ip_end = get_iplong('192.168.1.255');//至ip  
$ip = get_iplong('192.168.1.127');//判断的ip  
//可以这样简单判断  
if($ip>=$ip_start && $ip <=$ip_end){  
    echo &#39;IP在此范围内&#39;;  
}else{  
    echo &#39;IP不在此范围&#39;;  
}  
/** 
 * 将ip地址转换成int型 
 * @param $ip  ip地址 
 * @return number 返回数值 
 */  
function get_iplong($ip){  
    //bindec(decbin(ip2long(&#39;这里填ip地址&#39;)));  
    //ip2long();的意思是将IP地址转换成整型 ,  
    //之所以要decbin和bindec一下是为了防止IP数值过大int型存储不了出现负数。  
    return bindec(decbin(ip2long($ip)));  
}
Copy after login


The above is the detailed content of Determine whether an IP address exists within the 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