public function getipinfo($ip) {
$res = '其他';
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=".$ip;
$ch = curl_init();
$SSL = substr($url, 0, 8) == "https://" ? true : false;
if($SSL)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$output = curl_exec($ch);
curl_close($ch);
$res1 = preg_match("/({.*})/", $output,$matches);
if($matches){
$res2 = json_decode($matches[0], true);
$res = isset($res2['country'])?($res2['province']?$res2['province']:'其他'):'其他';
}
return $res;
}
private function check_useragent(){
$os = $browser = $language = '其他';
if(strpos($_SERVER["HTTP_USER_AGENT"],"iPhone"))
$os = "iPhone";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Android"))
$os = "Android";
if(strpos($_SERVER["HTTP_USER_AGENT"],"MicroMessenger"))
$browser = "Wechat";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE"))
$browser = "Internet Explorer";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Firefox"))
$browser = "Firefox";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Chrome"))
$browser = "Google Chrome";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Safari"))
$browser = "Safari";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Opera"))
$browser = "Opera";
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4); //只取前4位,这样只判断最优先的语言。如果取前5位,可能出现en,zh的情况,影响判断。
if (preg_match("/zh-c/i", $lang))
$language = "简体中文";
else if (preg_match("/zh/i", $lang))
$language = "繁體中文";
else if (preg_match("/en/i", $lang))
$language = "English";
else if (preg_match("/jp/i", $lang))
$language = "Japanese";
else if (preg_match("/ko/i", $lang))
$language = "Korean";
return array('os'=>$os,'browser'=>$browser,'language'=>$language);
}