php 根据IP查找地址的入门实例
内容摘要
这篇文章主要为大家详细介绍了php 根据IP查找地址的入门实例,具有一定的参考价值,可以用来参考一下。
php根据IP查找地址,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代
php根据IP查找地址,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代
文章正文
这篇文章主要为大家详细介绍了php 根据IP查找地址的入门实例,具有一定的参考价值,可以用来参考一下。
php根据IP查找地址,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <code class = "php" > /** * 根据IP查找地址 * * @param * @author php教程 www.idcnote.com **/ function detect_city( $ip ) { $default = 'UNKNOWN' ; if (! is_string ( $ip ) || strlen ( $ip ) < 1 || $ip == '127.0.0.1' || $ip == 'localhost' ) $ip = '8.8.8.8' ; $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)' ; $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode( $ip ); $ch = curl_init(); $curl_opt = array ( CURLOPT_FOLLOWLOCATION => 1, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_USERAGENT => $curlopt_useragent , CURLOPT_URL => $url , CURLOPT_TIMEOUT => 1, CURLOPT_REFERER => 'http://' . $_SERVER [ 'HTTP_HOST' ], ); curl_setopt_array( $ch , $curl_opt ); $content = curl_exec( $ch ); if (! is_null ( $curl_info )) { $curl_info = curl_getinfo( $ch ); } curl_close( $ch ); if ( preg_match( '{<li>City : ([^<]*)</li>}i' , $content , $regs ) ) { $city = $regs [1]; } if ( preg_match( '{<li>State/Province : ([^<]*)</li>}i' , $content , $regs ) ) { $state = $regs [1]; } if ( $city != '' && $state != '' ){ $location = $city . ', ' . $state ; return $location ; } else { return $default ; } } </code> |
注:关于php 根据IP查找地址的入门实例的内容就先介绍到这里,更多相关文章的可以留意
代码注释