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