日期:2014-05-17  浏览次数:20626 次

借用SINA IP地址库取用户所在城市及地区 PHP版

<?php
/*$ip=real_ip();
$url='http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='.$ip;
$content=fopen_url($url);
$Exp='/country":"(.*?)","province":"(.*?)","city":"(.*?)","district":"","isp":"(.*?)","type/s';
$content=unicodeDecode( $content );
if(preg_match($Exp,$content,$matche)){
echo 'Hello,来自'.$matche[1].$matche[2].$matche[3].'的朋友.';
}elseif(preg_match('/"ret":-1,"ip":"(.*?)"/',$content,$matche)){
echo '您使用的是局域网,局域网IP为'.$matche[1];
}else{
echo '你来自火星吧。';
}*/

/**
?*
?*/
function ipFunGetCityNameById(){
??? $url='http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='.ipFunReal_ip();
??? $content=ipFunFopen_url($url);
??? $Exp='/country":"(.*?)","province":"(.*?)","city":"(.*?)","district":"","isp":"(.*?)","type/s';
??? $content=ipFunUnicodeDecode( $content );
??? if(preg_match($Exp,$content,$matche)){
??? ??? return $matche[3];
??? }else
??? ??? return "";
}
?
?
//Curl 获取网址内容
function ipFunFopen_url($url)
{
??? if (function_exists('file_get_contents')) {
??????? $file_content = @file_get_contents($url);
??? } elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))){
??????? $i = 0;
??????? while (!feof($file) && $i++ < 1000) {
??????????? $file_content .= strtolower(fread($file, 4096));
??????? }
??????? fclose($file);
??? } elseif (function_exists('curl_init')) {
??????? $curl_handle = curl_init();
??????? curl_setopt($curl_handle, CURLOPT_URL, $url);
??????? curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
??????? curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
??????? curl_setopt($curl_handle, CURLOPT_FAILONERROR,1);
??????? curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check');
??????? $file_content = curl_exec($curl_handle);
??????? curl_close($curl_handle);
??? } else {
??????? $file_content = '';
??? }
??? return $file_content;
}
?
function ipFunUnicodeDecode($content)
{
??? // 转换编码,将Unicode编码转换gb2312编码
??? $pattern = '/(\\\u([\w]{4}))/i';
??? preg_match_all($pattern, $content, $matches);
??? if (!empty($matches))
??? {
??????? for ($j = 0; $j < count($matches[0]); $j++)
??????? {
??????????? $str = $matches[0][$j];
??????????? if (strpos($str, '\\u') === 0)
??????????? {
??????????????? $code = base_convert(substr($str, 2, 2), 16, 10);
??????????????? $code2 = base_convert(substr($str, 4), 16, 10);
?
??????????????? $c = chr($code).chr($code2);
??? ??? ??? ??? $c=mb_convert_encoding($c, "UTF-8", 'UCS-2');
??????????????? //$c = iconv('UCS-2', 'gb2312', $c);
??? ??? ??? ??? $content=str_replace($matches[0][$j],$c,$content);
??????????? }
??????????? else
??????????? {
??????????????? $content .= $str;
??????????? }
??????? }
??? }
??? return $content;
}
?
function ipFunReal_ip()
{
??? static $realip = NULL;
?
??? if ($realip !== NULL)
??? {
??????? return $realip;
??? }
?
??? if (isset($_SERVER))
??? {
??????? if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
??????? {
??????????? $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
?
??????????? /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
??????????? foreach ($arr AS $ip)
??????????? {
??????????????? $ip = trim($ip);
?
??????????????? if ($ip != 'unknown')
??????????????? {
??????????????????? $realip = $ip;
?
??????????????????? break;
??????????????? }
??????????? }
??????? }
??????? elseif (isset($_SERVER['HTTP_CLIENT_IP']))
??????? {
??????????? $realip = $_SERVER['HTTP_CLIENT_IP'];
??????? }
??????? else
??????? {
??????????? if (isset($_SERVER['RE