PHP implements IP address conversion to integer numeric instance _php tutorial

Source: Internet
Author: User
IP address translation in the integer data and then saved to the database, this is a common practice, we convert the IP algorithm is Intip = 256*256*256*w + 256*256*x + 256*y + Z can be, the following to give you students to introduce specific examples.

"Principle of conversion": Assuming that IP is: w.x.y.z, the IP address to integer number is calculated as: Intip = 256*256*256*w + 256*256*x + 256*y + Z

"PHP Mutual transfer": Php conversion method is relatively simple, it built up two functions

int Ip2long (String $ip _address)//IP converted to integral type value
string Long2ip (String $proper _address)//integer value converted to IP "MySQL cross-turn": Compared to MSSQL, MySQL is a simpler way to convert, and it has two functions built into PHP as well.

IP to Integer type:

Select Inet_aton (IP address) integer value converted to IP

Select Inet_ntoa (integer value for IP)


An instance

    1. Manual implementation of their own methods

function Ip2number ($IP)      {          $t = explode ('. ', $ip);          $x = 0;          for ($i = 0; $i < 4; $i + +)          {              $x = $x * + $t [$i];          }          return $x;      } function Number2ip ($num)      {          $t = $num;          $a = array ();          for ($i = 0; $i < 4; $i + +)          {              $x = $t%;              if ($x < 0) $x + =;              Array_unshift ($a, $x);              $t = Intval ($t/256);          }          Return implode ('. ', $a); }

http://www.bkjia.com/PHPjc/631516.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631516. Htmltecharticleip address translation in the integer data is then saved to the database, this is a common practice, we convert the IP algorithm is Intip = 256*256*256*w + 256*256*x + 256*y + z can, the following to each ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.