• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-24) 20次浏览 已收录 0个评论

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

<?php
/*$ip=real_ip();
$url=’http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=&#8217;.$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=&#8217;.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 = ‘@本文9来源gao($daima.com搞@代@#码8网^搞代gaodaima码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;
}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:挪用SINA IP地址库取用户所在城市及地区 PHP版
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址