IP address processing in PHP
In development, we always deal with ip addresses, and two functions are commonly used. .php provides the ip2long and long2ip methods for processing IP addresses. Today we will study it in detail. Let’s take a look at the ip processing method in PHP. Without further ado, let’s take a look at the ip address processing method in PHP!
In development, we always deal with IP addresses, and two functions are commonly used. PHP provides the ip2long and long2ip methods to process IP addresses.
ip2long converts an IPV4 string Internet protocol into a digital format
int ip2long ( string $ip_address ) //参数: ip_address 一个标准格式的地址。 //返回值: 返回IP地址转换后的数字 或 FALSE
long2ip converts a digital format into an IPV4 String Internet Protocol
string long2ip ( string $proper_address ) //参数: proper_address 长整型的正确地址表示。 //返回值: 返回互联网地址作为字符串。
When storing the IP address in the database, you can use the ip2long function to convert the IP address from a string to a long type to save storage space, and then use long2ip to restore the IP address when taking it out. After conversion, it is more convenient to judge whether the IP address is in the range, as follows:
$a=ip2long('127.0.0.66'); $b=ip2long('127.0.0.111'); $c=ip2long('127.0.0.10'); if($c>=$a && $c <=$b){ echo '呜啦啦'; }else{ echo '啦啦呜'; }
But you need to pay attention! ! ! Note Note Note
#The function ip2long() in php that converts IP into an integer is prone to problems. When the IP is relatively large, it will become a negative number.
Why? The answer is here!
IPv4 uses unsigned 32-bit addresses, so there are at most 2 to the 32nd power minus 1 (4294967295) addresses. Write decimal numbers separated by 4 decimal points.
is recorded as A.B.C.D, for example: 192.168.100.100.
Each decimal number in the IPv4 address is an unsigned byte, ranging from 0 to 255. Converting the IPv4 address to an unsigned number actually means converting each decimal number. The system number is placed on the corresponding 8 bits to form a 4-byte unsigned integer. 192.168.100.100, 192,168 in the high 8 digits and 100,100 in the low 8 digits.
As follows
<?php $ip = "192.168.1.2"; $ip_n = ip2long($ip); echo $ip_n; //输出的是个负数 -1062731518 ?>
Because the integer value converted from IP is too large and exceeds the range of the integer, it becomes a negative number.
Need to be written as $ip_n = bindec(decbin(ip2long($ip))); In this way, the unsigned integer number can be obtained, as follows
<?php $ip = "192.168.1.2"; $ip_n = bindec(decbin(ip2long($ip))); echo $ip_n; //得到 3232235778 ?>
There is also a solution:
Use %u to format unsigned integer when outputting.
<?php $ip = '192.168.101.100'; $ip_long = sprintf('%u',ip2long($ip)); echo $ip_long.PHP_EOL; // 3232261476 echo long2ip($ip_long); // 192.168.101.100 ?>
Related recommendations:
php ip2long causes negative numbers and solutions
php ip address conversion integer, integer conversion address
php IP Get City API (Innocent IP Database)
The above is the detailed content of IP address processing in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
