您好,欢迎访问一九零五行业门户网

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

一、获取浏览器信息,获取访客操作系统:windows、mac、linux、unix、bsd、other,以及访客ip地址等信息的php类
复制代码 代码如下:
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地理位置
复制代码 代码如下:
三、php利用新浪ip查询接口获取ip地理位置
复制代码 代码如下:
desc)) {     
      $loc = $location->province.$location->city.$location->district.$location->isp;   
   }else{         $loc = $location->desc;     
   }     
    return $loc; 

echo getiploc_sina(183.37.209.57); 
?>
http://www.bkjia.com/phpjc/760292.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/760292.htmltecharticle一、获取浏览器信息,获取访客操作系统:windows、mac、linux、unix、bsd、other,以及访客ip地址等信息的php类 复制代码 代码如下: ?php /** * 获...
其它类似信息

推荐信息