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 int in php

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to convert ip to int in php. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Php ip to int method: 1, create a PHP example file; 2, define an ip address; 3, through the ip2long function in PHP ip address conversion integer can be.

Operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

How to transfer php ip to int?

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

This paper introduces the use of PHP functions ip2long and long2ip, as well as their BUG introduction, and finally gives two algorithms written by oneself, which can be referred to by friends who need it.

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