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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
