php查询ip地址来源归属地的脚本

 1 <?php
 2     header('Content-Type:text/html;charset=utf-8');
 3     
 4     if($_GET['sub']){
 5         $ip = $_GET['ip'];
 6         $msg = '';
 7         ipSearch();
 8     }
 9     
10     function ipSearch(){
11         global $ip,$msg;
12         if(!$ip){
13             $msg = '请至少填写一个ip!';
14             return;
15         }
16         $arises = substr_count($ip,'.');
17         $long = ip2long($ip);
18         if($arises != 3 || $long == false || $long== -1){
19             $msg = '无效ip地址,请重新输入!';
20             return;
21         }
22         $content = file_get_contents('http://www.ip138.com/ips1388.asp?action=2&ip='.$ip);
23         $content = iconv('gb2312', 'utf-8', $content);
24         $pos = stripos($content, '<li>本站主数据');
25         if(!$pos){
26             $msg = '没有查询到!';
27             return;
28         }
29         $endPos = stripos($content, '</li>', $pos);
30         $jumpLen = strlen('<li>本站主数据:');
31         $address = substr($content, $pos+$jumpLen, $endPos-$pos-$jumpLen);
32         $msg = $address;
33         return;
34     }
35     
36 ?><!DOCTYPE html>
37 <html>
38 <head>
39     <meta charset="utf-8">
40     <title>test</title>
41 </head>
42 <body>
43     <form method="get">
44         <input type="text" name="ip" />
45         <input type="submit" value="search" name="sub" />
46     </form><br />
47     <?php echo $msg; ?>
48 </body>
49 </html>
原文地址:https://www.cnblogs.com/chenyanger/p/3682934.html