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

简介:这是借用SINA IP地址库取用户所在城市及地区 PHP版的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=327018' scrolling='no'>

<?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;
}

“借用SINA IP地址库取用户所在城市及地区 PHP版”的更多相关文章 》

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/327018.html pageNo:12
原文地址:https://www.cnblogs.com/ooooo/p/2250885.html