Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert ip to Integer by php

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "php how to achieve ip to integer", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "php how to achieve ip to integer" bar!

Php to achieve ip to integer methods: 1, to create a PHP example file; 2, through the "ip2long ($ip);" method to convert ip addresses into integers.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How does php convert ip to integers?

Detailed explanation of the conversion between IP address and Integer number in PHP

This article mainly introduces the detailed explanation of the conversion between IP addresses and integer numbers in PHP. This paper introduces the use of PHP functions ip2long and long2ip, as well as their BUG. Finally, two algorithms written by yourself are given, which can be referred to by friends who need them.

Converting IP to integer storage is a major trend in database optimization. Many people still use string storage when storing IP. String indexing consumes more resources than integer indexing, especially when there is a large amount of data in the table, and when querying the data of a certain ip segment, today's ip means that ip4,ip6 is not within the scope of this article.

System functions ip2long and long2ip

There is a built-in function ip2long in PHP that converts ip addresses into integers.

The code is as follows:

$ip = '210.110.11.49 potential echo ip2long ($ip)

Output:

-764540111

The output integer has a minus sign because the result we get is a signed integer, with a maximum value of 2147483647. To convert the result to an unsigned type, you can write this.

3530427185

Use long2ip to convert integers back to ip addresses

$ip = '210.110.11.49); echo long2ip ($ip); echo $ip. "; echo $ip_int."; echo long2ip ($ip_int)

Output:

210.110.011.49-764540623210.110.9.49

The conversion results do not match. Let's try to precede the first number of ip with a leading 0, and then take a look.

$ip = '021.110.11.49); echo long2ip ($ip); echo $ip. "; echo $ip_int."; echo long2ip ($ip_int)

Output:

021.110.11.4929242449717.110.11.49

The conversion results are all wrong. The above examples are all due to the error of the conversion result caused by the addition of leading 0, and the associated reversal result does not match the original conversion ip.

Conversion principle

There are currently two algorithms:

The first paragraph times 256 to the third power, the second paragraph times 256 squared, the third paragraph multiplied by 256, and the final sum.

$ip = '0210.110.11.49'; function ipToInt ($ip) {$iparr = explode ('.', $ip); $num = 0; for ($item0

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report