• PHP获取客户端的IP、地理信息、浏览器、本地真实IP


    <?php
    
    header("Content-type:text/html;charset=utf-8");
    
    // 作用获取客户端的ip、地理信息、浏览器、本地真实IP
    class get_gust_info { 
           
        //获得访客浏览器类型
        function GetBrowser(){
            if(!empty($_SERVER['HTTP_USER_AGENT'])) {
                $br = $_SERVER['HTTP_USER_AGENT'];
                if (preg_match('/MSIE/i',$br)) {    
                    $br = 'MSIE';
                }elseif (preg_match('/Firefox/i',$br)) {
                    $br = 'Firefox';
                }elseif (preg_match('/Chrome/i',$br)) {
                    $br = 'Chrome';
                }elseif (preg_match('/Safari/i',$br)) {
                    $br = 'Safari';
                }elseif (preg_match('/Opera/i',$br)) {
                    $br = 'Opera';
                }elseif (preg_match('/QQBrowser/i',$br)) {
                    $br = 'QQBrowser';
                }elseif (preg_match('/UCBrowser/i',$br)) {
                    $br = 'UCBrowser';
                }else {
                    $br = 'Other';
                }
                return $br;
            }else {
                return false;
            }
        }
    
        //获得访客浏览器语言
        function GetLang() {
            if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
                $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
                $lang = substr($lang,0,5);
                if(preg_match("/zh-cn/i",$lang)) {
                    $lang = "简体中文";
                }elseif(preg_match("/zh/i",$lang)) {
                    $lang = "繁体中文";
                }else{
                    $lang = "English";
                }
                return $lang;
            }else {
                return false;
            }
        }
    
        //获取访客操作系统
        function GetOs(){
            if(!empty($_SERVER['HTTP_USER_AGENT'])) {
                $OS = $_SERVER['HTTP_USER_AGENT'];
                if (strpos($OS, 'Android') !== false) {//strpos()定位出第一次出现字符串的位置,这里定位为0  
                    preg_match("/(?<=Android )[d.]{1,}/", $OS, $version);  
                    $OS ='Android '.$version[0];
                }elseif (strpos($OS, 'iPhone') !== false) {  
                    preg_match("/(?<=CPU iPhone OS )[d\_]{1,}/", $OS, $version);  
                    $OS = 'iPhone iOS '.str_replace('_', '.', $version[0]);  
                }elseif (strpos($OS, 'iPad') !== false) {  
                    preg_match("/(?<=CPU OS )[d\_]{1,}/", $OS, $version);  
                    $OS = 'iPad iOS '.str_replace('_', '.', $version[0]);   
                }elseif (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;  
            }else {
                return false;
            }   
        }
    
        //获得访客真实ip
        function Getip() {
            $ip = false;
            if (getenv("HTTP_CLIENT_IP")) {
                $ip = getenv("HTTP_CLIENT_IP");
            }elseif (getenv("HTTP_X_FORWARDED_FOR")) {
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            }elseif (getenv("REMOTE_ADDR")) {
                $ip = getenv("REMOTE_ADDR");
            }
            return $ip;
        }
    
        //根据ip获得访客所在地地名
        function Getaddress($ip='') {
            if(empty($ip)) {
                $ip = $this->Getip();    
            }
            $ipadd = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=".$ip);//根据新浪api接口获取
            if($ipadd) {
                $charset = iconv("gbk","utf-8",$ipadd);   
                preg_match_all("/[x{4e00}-x{9fa5}]+/u",$charset,$ipadds);
             
                return $ipadds;   //返回一个二维数组
            }else {
                return "address is none";
            }  
        }
    
    }
    
    $gifo = new get_gust_info();
    
    echo "浏览器类型:".$gifo->GetBrowser().'<br>';
    echo "浏览器语言:".$gifo->GetLang().'<br>';
    echo "操作系统:".$gifo->GetOs().'<br>';
    echo "你的ip:".$gifo->getIP().'<br>';
    echo "所在地:";
    $address = $gifo->Getaddress($gifo->getIP());
    foreach ($address[0] as $key) {
        echo $key.' ';
    }
    
    ?>
    

      

  • 相关阅读:
    python-Python调用wcf接口
    一个数据驱动的ui自动化框架思路
    selenium分布式部署
    UI自动化-Element is not clickable at point-----问题记录
    idea下载git代码
    windows的hosts文件路径
    端口号
    Hadoop压缩
    MongoDB(单节点)环境配置
    快排
  • 原文地址:https://www.cnblogs.com/pengyunjing/p/6811166.html
Copyright © 2020-2023  润新知