Home Backend Development PHP Tutorial IP address processing in PHP

IP address processing in PHP

Dec 02, 2017 am 10:54 AM
php address deal with

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
Copy after login

long2ip converts a digital format into an IPV4 String Internet Protocol

string long2ip ( string $proper_address )
//参数: proper_address 长整型的正确地址表示。
//返回值: 返回互联网地址作为字符串。
Copy after login

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 &#39;呜啦啦&#39;;
}else{
echo &#39;啦啦呜&#39;;
}
Copy after login

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
?>
Copy after login

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
?>
Copy after login

There is also a solution:

Use %u to format unsigned integer when outputting.

<?php
$ip = &#39;192.168.101.100&#39;;
$ip_long = sprintf(&#39;%u&#39;,ip2long($ip));
echo $ip_long.PHP_EOL;  // 3232261476 
echo long2ip($ip_long); // 192.168.101.100
?>
Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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 Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

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.

See all articles