Home > php教程 > PHP源码 > body text

PHP获取访客IP和地理位置等信息的类

PHP中文网
Release: 2016-05-25 17:13:12
Original
1425 people have browsed it

<?php   
/**  
 * 获取访客信息的类:语言、浏览器、操作系统、IP、地理位置、ISP。  
 * 日期:2011-09-13  
 * 作者:www.itwhy.org  
 * 使用:  
 *      $obj = new class_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 class_guest_info{   
    function GetLang() {   
        $Lang = substr($_SERVER[&#39;HTTP_ACCEPT_LANGUAGE&#39;], 0, 4);   
        //使用substr()截取字符串,从 0 位开始,截取4个字符   
        if (preg_match(&#39;/zh-c/i&#39;,$Lang)) {   
        //preg_match()正则表达式匹配函数   
            $Lang = &#39;简体中文&#39;;   
        }   
        elseif (preg_match(&#39;/zh/i&#39;,$Lang)) {   
            $Lang = &#39;繁體中文&#39;;   
        }   
        else {   
            $Lang = &#39;English&#39;;   
        }   
        return $Lang;   
    }   
    function GetBrowser() {   
        $Browser = $_SERVER[&#39;HTTP_USER_AGENT&#39;];   
        if (preg_match(&#39;/MSIE/i&#39;,$Browser)) {   
            $Browser = &#39;MSIE&#39;;   
        }   
        elseif (preg_match(&#39;/Firefox/i&#39;,$Browser)) {   
            $Browser = &#39;Firefox&#39;;   
        }   
        elseif (preg_match(&#39;/Chrome/i&#39;,$Browser)) {   
            $Browser = &#39;Chrome&#39;;   
        }   
        elseif (preg_match(&#39;/Safari/i&#39;,$Browser)) {   
            $Browser = &#39;Safari&#39;;   
        }   
        elseif (preg_match(&#39;/Opera/i&#39;,$Browser)) {   
            $Browser = &#39;Opera&#39;;   
        }   
        else {   
            $Browser = &#39;Other&#39;;   
        }   
        return $Browser;   
    }   
    function GetOS() {   
        $OS = $_SERVER[&#39;HTTP_USER_AGENT&#39;];   
        if (preg_match(&#39;/win/i&#39;,$OS)) {   
            $OS = &#39;Windows&#39;;   
        }   
        elseif (preg_match(&#39;/mac/i&#39;,$OS)) {   
            $OS = &#39;MAC&#39;;   
        }   
        elseif (preg_match(&#39;/linux/i&#39;,$OS)) {   
            $OS = &#39;Linux&#39;;   
        }   
        elseif (preg_match(&#39;/unix/i&#39;,$OS)) {   
            $OS = &#39;Unix&#39;;   
        }   
        elseif (preg_match(&#39;/bsd/i&#39;,$OS)) {   
            $OS = &#39;BSD&#39;;   
        }   
        else {   
            $OS = &#39;Other&#39;;   
        }   
        return $OS;   
    }   
    function GetIP() {   
        if (!emptyempty($_SERVER[&#39;HTTP_CLIENT_IP&#39;])) {   
        //如果变量是非空或非零的值,则 empty()返回 FALSE。   
            $IP = explode(&#39;,&#39;,$_SERVER[&#39;HTTP_CLIENT_IP&#39;]);   
        }   
        elseif (!emptyempty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;])) {   
            $IP = explode(&#39;,&#39;,$_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]);   
        }   
        elseif (!emptyempty($_SERVER[&#39;REMOTE_ADDR&#39;])) {   
            $IP = explode(&#39;,&#39;,$_SERVER[&#39;REMOTE_ADDR&#39;]);   
        }   
        else {   
            $IP[0] = &#39;None&#39;;   
        }   
        return $IP[0];   
    }   
    private function GetAddIsp() {   
        $IP = $this->GetIP();   
        $AddIsp = mb_convert_encoding(file_get_contents(&#39;http://open.baidu.com/ipsearch/s?tn=ipjson&wd=&#39;.$IP),&#39;UTF-8&#39;,&#39;GBK&#39;);   
        //mb_convert_encoding() 转换字符编码。   
        if (preg_match(&#39;/noresult/i&#39;,$AddIsp)) {   
            $AddIsp = &#39;None&#39;;   
        }   
        else {   
            $Sta = stripos($AddIsp,$IP) + strlen($IP) + strlen(&#39;来自&#39;);   
            $Len = stripos($AddIsp,&#39;"}&#39;)-$Sta;   
            $AddIsp = substr($AddIsp,$Sta,$Len);   
        }   
        $AddIsp = explode(&#39; &#39;,$AddIsp);   
        return $AddIsp;   
    }   
    function GetAdd() {   
        $Add = $this->GetAddIsp();   
        return $Add[0];   
    }   
    function GetIsp() {   
        $Isp = $this->GetAddIsp();   
        if ($Isp[0] != &#39;None&#39; && isset($Isp[1])) {   
            $Isp = $Isp[1];   
        }   
        else {   
            $Isp = &#39;None&#39;;   
        }   
        return $Isp;   
    }   
}   
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!