PHP code to implement IP address and number exchange
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>PHP中文网</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <body> <?php /* * 作者:XXXX */ //将IP转换为数字 function ipton($ip) { $ip_arr=explode('.',$ip);//分隔ip段 foreach ($ip_arr as $value) { $iphex=dechex($value);//将每段ip转换成16进制 if(strlen($iphex)<2)//255的16进制表示是ff,所以每段ip的16进制长度不会超过2 { $iphex='0'.$iphex;//如果转换后的16进制数长度小于2,在其前面加一个0 //没有长度为2,且第一位是0的16进制表示,这是为了在将数字转换成ip时,好处理 } $ipstr.=$iphex;//将四段IP的16进制数连接起来,得到一个16进制字符串,长度为8 } return hexdec($ipstr);//将16进制字符串转换成10进制,得到ip的数字表示 } //将数字转换为IP,进行上面函数的逆向过程 function ntoip($n) { $iphex=dechex($n);//将10进制数字转换成16进制 $len=strlen($iphex);//得到16进制字符串的长度 if(strlen($iphex)<8) { $iphex='0'.$iphex;//如果长度小于8,在最前面加0 $len=strlen($iphex); //重新得到16进制字符串的长度 } //这是因为ipton函数得到的16进制字符串,如果第一位为0,在转换成数字后,是不会显示的 //所以,如果长度小于8,肯定要把第一位的0加上去 //为什么一定是第一位的0呢,因为在ipton函数中,后面各段加的'0'都在中间,转换成数字后,不会消失 for($i=0,$j=0;$j<$len;$i=$i+1,$j=$j+2) {//循环截取16进制字符串,每次截取2个长度 $ippart=substr($iphex,$j,2);//得到每段IP所对应的16进制数 $fipart=substr($ippart,0,1);//截取16进制数的第一位 if($fipart=='0') {//如果第一位为0,说明原数只有1位 $ippart=substr($ippart,1,1);//将0截取掉 } $ip[]=hexdec($ippart);//将每段16进制数转换成对应的10进制数,即IP各段的值 } $ip = array_reverse($ip); return implode('.', $ip);//连接各段,返回原IP值 } echo ipton('119.255.31.226'); echo '<br>'; $num='379374783'; echo strlen($num).'<br/>'; echo ntoip($num).'<br/>'; echo 'trueipnum:'.ip2long('119.255.31.226').'<br/>'; echo 'trueip:'.long2ip('3793747831'); ?> </body> </html>
mysql comes with the inet_aton function to convert the ip address into a number, and the inet_ntoa function to convert the number into an ip.
SELECT INET_ATON('10.122.22.1')
The above conversion function is original by the webmaster. It happens to achieve the same conversion effect as the mysql system function. Haha, I am excited for a long time~~
It turns out that PHP provides ip2long and long2ip2 Function to realize the conversion of IP and numbers, the webmaster has done a useless job, but it should be exercised logical thinking~~
These are two implementation methods, one is to implement it programmatically; the other is to implement it directly in the sql statement , very convenient and good, I saved it and posted it to my blog, so I won’t forget it in the future.
When saving IP data in the database (MySQL), we are used to using the ip2long function to generate an integer and then store it in an int(11) type field. However, on different system platforms, the ip2long function obtains The values are different, so it may cause errors when reading data from the database and using long2ip to get the IP. Let’s talk about the situation we encountered:
We use an int (11) type (range - 2147483648 - 2147483647) Save the result of using ip2long to process an ip address. For example, if the ip is '202.105.77.179', then the result obtained on a 32-bit machine is: -899068493, but on a 64-bit machine, 3395898803 is obtained. Then write it Because the database exceeds the range of int(11), the result on the 64-bit machine is saved as the maximum value of int(11): 2147483647. So when it is taken out from the database, the wrong result is obtained, and " 127.255.255.255″This IP address.
There are many solutions. For example, you can use mysql functions: INET_ATON and INET_NTOA to process the IP address; or change the field that saves the IP address to a bigint type, so that it can be saved on a 64-bit machine. It is 3395898803, and you can still get the correct result using the long2ip function.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:
