search
Homephp教程php手册PHP中如何将ip地址转成十进制数

   PHP 中如何将ip地址转成十进制数呢?现在PHP中有很多时候都会用到ip地址,但是这个ip地址获取的时候都不是10进制的。那么PHP中如何将ip地址转成十进制数就是我们比较头疼的事情了,下面两种方法是我整理处理来相对比较简单的IP地址转成十进制数的方法。希望能对大家有所帮助。

  方法一:

  代码如下

  public function ipToLong(){

  $ip = $_SERVER['REMOTE_ADDR'];

  $ip = explode('.', $ip);

  $ip = array_reverse($ip);//数组反转

  $r = 0;

  for($i=0,$j=count($ip); $i

  $r += $ip[$i] * pow(256, $i);

  }

  $r = sprintf("%u", $r);

  echo $r;

  }

  方法二:

  代码如下

  public function ipToLong(){

  $ip = $_SERVER['REMOTE_ADDR'];

  $ip = explode('.',$ip);

  $r = ($ip[0]

  if($r

  echo $r ;

  }

  两个结果在本地服务器中的结果都是3232235877,运用的ip是192.168.1.101。我们用ping 192.168.1.101 和 ping 3232235877来进行检测,看是否一样。

  可能有些朋友不知道mysql与php中都提供了IP转换十进制数函数

  MySQL 提供了INET_ATON 和 INET_NTOA 两个函数来进行地址和整数之间的转换。

  1. IP 由点分格式,转换为数字格式。

  代码如下

  mysql> select inet_aton('127.0.0.1');

  +------------------------+

  | inet_aton('127.0.0.1') |

  +------------------------+

  | 2130706433 |

  +------------------------+

  1 row in set (0.00 sec)

  2. IP 由数字格式转换为点分格式。

  代码如下

  mysql> select inet_ntoa(2130706433);

  +-----------------------+

  | inet_ntoa(2130706433) |

  +-----------------------+

  | 127.0.0.1 |

  +-----------------------+

  1 row in set (0.00 sec)

  php可直接使用ip2long函数

  代码如下

  echo ip2long('192.168.1.38');

  输出:3232235814

Statement
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

Video Face Swap

Video Face Swap

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

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),