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

借用SINA IP地址库取用户所在城市及地区 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['REMOTE_ADDR']))

{

$realip = $_SERVER['REMOTE_ADDR'];

}

else

{

$realip = '0.0.0.0';

}

}

}

else

{

if (getenv('HTTP_X_FORWARDED_FOR'))

{

$realip = getenv('HTTP_X_FORWARDED_FOR');

}

elseif (getenv('HTTP_CLIENT_IP'))

{

$realip = getenv('HTTP_CLIENT_IP');

}

else

{

$realip = getenv('REMOTE_ADDR');

}

}

preg_match("/[\d\.]{7,15}/", $realip, $onlineip);

$realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';

return $realip;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值