PHP头条
热点:

PHP获取浏览器信息类和客户端地理位置的2个方法


一、获取浏览器信息,获取访客操作系统:windows、mac、linux、unix、bsd、other,以及访客ip地址等信息的PHP类
代码如下:
/**
* 获取访客信息的类:语言、浏览器、操作系统、ip、地理位置、isp。
* 使用:
* $obj = new guest_info;
* $obj->getlang(); //获取访客语言:简体中文、繁體中文、english。
* $obj->getbrowser(); //获取访客浏览器:msie、firefox、chrome、safari、opera、other。
* $obj->getos(); //获取访客操作系统:windows、mac、linux、unix、bsd、other。
* $obj->getip(); //获取访客ip地址。
* $obj->getadd(); //获取访客地理位置,使用 baidu 隐藏接口。
* $obj->getisp(); //获取访客isp,使用 baidu 隐藏接口。
*/
class guest_info{
function getlang() {
$lang = substr($_server['http_accept_language'], 0, 4);
//使用substr()截取字符串,从 0 位开始,截取4个字符
if (preg_match('/zh-c/i',$lang)) {
//preg_match()正则表达式匹配函数
$lang = '简体中文';
}
elseif (preg_match('/zh/i',$lang)) {
$lang = '繁體中文';
}
else {
$lang = 'english';
}
return $lang;
}
function getbrowser() {
$browser = $_server['http_user_agent'];
if (preg_match('/msie/i',$browser)) {
$browser = 'msie';
}
elseif (preg_match('/firefox/i',$browser)) {
$browser = 'firefox';
}
elseif (preg_match('/chrome/i',$browser)) {
$browser = 'chrome';
}
elseif (preg_match('/safari/i',$browser)) {
$browser = 'safari';
}
elseif (preg_match('/opera/i',$browser)) {
$browser = 'opera';
}
else {
$browser = 'other';
}
return $browser;
}
function getos() {
$os = $_server['http_user_agent'];
if (preg_match('/win/i',$os)) {
$os = 'windows';
}
elseif (preg_match('/mac/i',$os)) {
$os = 'mac';
}
elseif (preg_match('/linux/i',$os)) {
$os = 'linux';
}
elseif (preg_match('/unix/i',$os)) {
$os = 'unix';
}
elseif (preg_match('/bsd/i',$os)) {
$os = 'bsd';
}
else {
$os = 'other';
}
return $os;
}
function getip() {
if (!empty($_server['http_client_ip'])) {
//如果变量是非空或非零的值,则 empty()返回 false。
$ip = explode(',',$_server['http_client_ip']);
}
elseif (!empty($_server['http_x_forwarded_for'])) {
$ip = explode(',',$_server['http_x_forwarded_for']);
}
elseif (!empty($_server['remote_addr'])) {
$ip = explode(',',$_server['remote_addr']);
}
else {
$ip[0] = 'none';
}
return $ip[0];
}
}

$obj = new guest_info;
echo $obj->getlang(); //获取访客语言:简体中文、繁體中文、english。
echo $obj->getbrowser(); //获取访客浏览器:msie、firefox、chrome、safari、opera、other。
echo $obj->getos(); //获取访客操作系统:windows、mac、linux、unix、bsd、other。
echo $obj->getip(); //获取访客ip地址。
?>


二、php利用腾讯ip分享计划获取ip地理位置

代码如下:
function getiploc_qq($queryip){
$url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryip;
$ch = curl_init($url);
curl_setopt($ch,curlopt_encoding ,'gb2312');
curl_setopt($ch, curlopt_timeout, 10);
curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回
$result = curl_exec($ch);
$result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换,否则乱码
curl_close($ch);
preg_match("@(.*)

@iu",$result,$iparray);
$loc = $iparray[1];
return $loc;
}
//使用
echo getiploc_qq("183.37.209.57"); //即可得到ip地址所在的地址位置。
?>

三、php利用新浪ip查询接口获取ip地理位置

代码如下:
function getiploc_sina($queryip){
$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$queryip;
$ch = curl_init($url);
curl_setopt($ch,curlopt_encoding ,'utf8');
curl_setopt($ch, curlopt_timeout, 5);
curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回
$location = curl_exec($ch);
$location = json_decode($location);
curl_close($ch);
$loc = "";
if($location===false) return "";
if (empty($location->desc)) {
$loc = $location->province.$location->city.$location->district.$location->isp;
}else{ $loc = $location->desc;
}
return $loc;
}
echo getiploc_sina("183.37.209.57");
?>

www.phpzy.comtrue/phpyy/27754.htmlTechArticlePHP获取浏览器信息类和客户端地理位置的2个方法 一、获取浏览器信息,获取访客操作系统:windows、mac、linux、unix、bsd、other,以及访客ip地址等信息的PHP类 代码如下: /** * 获取访客信息...

相关文章

PHP之友评论

今天推荐