PHP function ip2long() implements a solution to the problem of negative numbers when converting IP to integer

WBOY
Release: 2016-07-25 08:57:40
Original
1595 people have browsed it
  1. $ip = "192.168.1.2";
  2. $ip_n = ip2long($ip);
  3. echo $ip_n; //Get -1062731518
  4. ?>
Copy code

Reason: The integer value converted by IP is too large and exceeds the range of the integer, so it becomes a negative number.

You need to make the following modifications, modify it to $ip_n = bindec(decbin(ip2long($ip))) to get the unsigned integer.

For example:

  1. $ip = "192.168.1.2";
  2. $ip_n = bindec(decbin(ip2long($ip)));
  3. //by bbs.it-home.org
  4. echo $ ip_n; //Get 3232235778
  5. ?>
Copy code


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!