Home  >  Article  >  Backend Development  >  How to convert php ip to number

How to convert php ip to number

藏色散人
藏色散人Original
2020-11-01 13:58:083042browse

php How to convert ip to number: 1. Use the PHP function ip2long function and sprintf function to convert; 2. Convert through the "gmp_strval(gmp_init($ipv6long,2),10);" method.

How to convert php ip to number

Recommended: "PHP Video Tutorial"

Convert ip to number in PHP (ipv4/ipv6)

Requirement: Convert the ip address of ipv4 or ivp6 to a number and store it in the database

1. Convert ipv4 to a number (positive integer):

ipv4 can be used directly The function ip2long() that comes with PHP is combined with the sprintf() function for conversion. The code is as follows:

$ip = '202.203.44.225';
$ipToInt = sprintf('%u',ip2long($ip)); // 结果为:3402312929
echo "ip '202.203.44.225' to int is: ".$ipToInt;

The result is:

How to convert php ip to number

ip2long() function It is to convert ip to a long integer number, but some ip is a complex number when converted to a long integer number, such as ip:, so you need to use the sprintf() function to convert the array into a positive integer. There are other methods to convert complex numbers into positive integers, but I think sprintf() is the most convenient method, so I recommend you use sprintf() here. In addition, I remind everyone not to use the abs() function to convert negative numbers, because the converted results have certain differences, such as:

$ip = '192.168.101.100';
$ip2int = ip2long($ip); 
echo abs($ip2int).&#39;<br/>&#39;; // 结果为:1062705820
echo sprintf(&#39;%u&#39;,$ip2int); // 结果为:3232261476

2. Convert ipv6 to numbers (positive integers):

For the ip address in ipv6 format, we cannot use the ip2long() function to directly convert it. We can only convert it through other methods. The following will be posted by netizen Weissner in the PHP manual (http://php.net/manual/zh/function .ip2long.php#94477) leaving a solution. Before that, everyone needs to pay attention to the fact that the comment symbol ';' before extension=php_gmp.dll needs to be removed in the php.ini file, because it will be used in the code. Change the function in the extension. .

The code to convert ipv6 to numbers is as follows:

$ipv6 = &#39;FEDC:BA98:7654:3210:FEDC:BA98:7654:3210&#39;;
$ip_n = inet_pton($ipv6);
$bits = 15; // 16 x 8 bit = 128bit
$ipv6long=&#39;&#39;;
while ($bits >= 0) {
        $bin = sprintf("%08b",(ord($ip_n[$bits])));
        $ipv6long = $bin.$ipv6long;
        $bits--;
}
echo gmp_strval(gmp_init($ipv6long,2),10); // 结果为:338770000845734292534325025077361652240

As you can see, after the ipv6 address is converted to numbers, there are 38 numbers, and the bigint in the database cannot be saved, so You also need to convert the field type in the database to char or varchar type.

Attached is the code to determine whether the ip is ivp4 or ipv6, and convert the ip to digital format:

$ip = &#39;192.168.101.100&#39;;
//$ip = &#39;FEDC:BA98:7654:3210:FEDC:BA98:7654:3210&#39;;
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    echo sprintf(&#39;%u&#39;,ip2long($ip));exit;
 
} else if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
 
$ip_n = inet_pton($ip);
$bits = 15; // 16 x 8 bit = 128bit
$ipv6long=&#39;&#39;;
    while ($bits >= 0) {
        $bin = sprintf("%08b",(ord($ip_n[$bits])));
        $ipv6long = $bin.$ipv6long;
        $bits--;
    }
    echo gmp_strval(gmp_init($ipv6long,2),10);exit;
}

The above is the detailed content of How to convert php ip to number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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