If you build your own IP database to occupy resources and the update is inconvenient, why not use a ready-made IP address for query? Below I wrote a PHP code for obtaining the IP address's physical address (please kindly advise if you have any flaws)
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html lang = "en">
<Head>
<Title> </title>
<Meta charset = "UTF-8"/>
<Meta name = "author" content = ""/>
<Meta name = "keywords" content = ""/>
<Meta name = "description" content = ""/>
</Head>
<Body>
<? Php
$ IpAddr = "218.75.124.100 ";
$ Ip138Addr = "http://www.ip138.com/ips8.asp? Ip = ". $ ipAddr." & action = 2 ";
$ Contents = file_get_contents ($ ip138Addr );
$ Pattern = "/\ <li. +? \/Li \> /";
$ String = $ contents;
Preg_match_all ($ pattern, $ string, $ addrArray );
$ Num = count ($ addrArray [0]);
For ($ I = 0; $ I <$ num; $ I ++ ){
Echo $ addrArray [0] [$ I] = mb_convert_encoding ($ addrArray [0] [$ I], "UTF-8", "GB2312 ");
Switch ($ I ){
Case 0: $ str = "<li> main site data: </li>"; break;
Case 1: $ str = "<li> reference data 1: </li>"; break;
Case 2: $ str = "<li> reference data 2: </li>"; break;
Case 3: $ str = "<li> reference data 3: </li>"; break;
Case 4: $ str = "<li> reference data 4: </li>"; break;
Case 5: $ str = "<li> reference data 6: </li>"; break;
Case 6: $ str = "<li> reference data 7: </li>"; break;
}
$ AddrArray [0] [$ I] = trim ($ addrArray [0] [$ I], $ str );
}
$ AddrArray [0] [0] = trim ($ addrArray [0] [0], ":");
Echo "<pre> ";
Print_r ($ addrArray );
Echo "</pre> ";
?>
</Body>
</Html>
This Code uses access to the http://www.ip138.com using a regular expression to intercept part of the content, where the regular expression is self-written.
Fortunately, the following processing results contain garbled characters. If $ str (the first and last character of the string is removed) is defined, no garbled characters are found. In addition, a separate ": "statement, you can achieve the phenomenon of no garbled, do not know why ......
Regular Expression -- Obtain the physical address of the specified IP address (2) chinaz data
The result obtained from the previous IP address is not perfect. The reason is that the IP Address Resolution of ip138 is not very detailed in dealing with foreign addresses, So I re-wrote a code using Chianz.com.
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html lang = "en">
<Head>
<Title> </title>
<Meta charset = "UTF-8"/>
<Meta name = "author" content = ""/>
<Meta name = "keywords" content = ""/>
<Meta name = "description" content = ""/>
</Head>
<Body>
<? Php
$ IpAddr = "4.4.44.4 ";
$ IpChinazAddr = "http://tool.chinaz.com/IP? IP = ". $ ipAddr;
$ Contents = file_get_contents ($ ipChinazAddr );
$ Pattern = "/\=\\>\>. + \ <\/strong \ >\< br \/\> /";
$ String = $ contents;
Preg_match_all ($ pattern, $ string, $ addrArray );
$ Num = count ($ addrArray [0]);
$ Pattern = "/(==>>). + (==>>> \ s )/";
For ($ I = 0; $ I <$ num; $ I ++ ){
$ AddrArray [0] [$ I] = preg_replace ($ pattern, "", $ addrArray [0] [$ I]);
}
Echo "<pre> ";
Print_r ($ addrArray );
Echo "</pre> ";
?>
</Body>
</Html>
Satisfactory results
The above are two codes written by myself to obtain the physical IP address. As a beginner, it is quite interesting to write simple code when it is okay. After all, I am a beginner and cannot pay more attention to security, space utilization, time efficiency, and other issues. I hope you can leave your footprints here. Thank you, younger brother.