PHP class library to obtain client IP, geographical information, browser, and real IP

不言
Release: 2023-03-24 17:36:01
Original
1406 people have browsed it

This article mainly introduces the PHP class library for obtaining client IP, geographical information, browser, and real IP. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

  1. <?php
    //作用取得客户端的ip、地理信息、浏览器、本地真实IP
     class get_gust_info { 
      ////获得访客浏览器类型
      function GetBrowser(){
       if(!empty($_SERVER[&#39;HTTP_USER_AGENT&#39;])){
        $br = $_SERVER[&#39;HTTP_USER_AGENT&#39;];
        if (preg_match(&#39;/MSIE/i&#39;,$br)) {    
                   $br = &#39;MSIE&#39;;
                 }elseif (preg_match(&#39;/Firefox/i&#39;,$br)) {
         $br = &#39;Firefox&#39;;
        }elseif (preg_match(&#39;/Chrome/i&#39;,$br)) {
         $br = &#39;Chrome&#39;;
           }elseif (preg_match(&#39;/Safari/i&#39;,$br)) {
         $br = &#39;Safari&#39;;
        }elseif (preg_match(&#39;/Opera/i&#39;,$br)) {
            $br = &#39;Opera&#39;;
        }else {
            $br = &#39;Other&#39;;
        }
        return $br;
       }else{return "获取浏览器信息失败!";} 
      }
       
      ////获得访客浏览器语言
      function GetLang(){
       if(!empty($_SERVER[&#39;HTTP_ACCEPT_LANGUAGE&#39;])){
        $lang = $_SERVER[&#39;HTTP_ACCEPT_LANGUAGE&#39;];
        $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 "获取浏览器语言失败!";}
      }
       
       ////获取访客操作系统
      function GetOs(){
       if(!empty($_SERVER[&#39;HTTP_USER_AGENT&#39;])){
        $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;  
       }else{return "获取访客操作系统信息失败!";}   
      }
       
      ////获得访客真实ip
      function Getip(){
       if(!empty($_SERVER["HTTP_CLIENT_IP"])){   
          $ip = $_SERVER["HTTP_CLIENT_IP"];
       }
       if(!empty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;])){ //获取代理ip
        $ips = explode(&#39;,&#39;,$_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]);
       }
       if($ip){
          $ips = array_unshift($ips,$ip); 
       }
        
       $count = count($ips);
       for($i=0;$i<$count;$i++){   
         if(!preg_match("/^(10|172\.16|192\.168)\./i",$ips[$i])){//排除局域网ip
          $ip = $ips[$i];
          break;    
          }  
       }  
       $tip = empty($_SERVER[&#39;REMOTE_ADDR&#39;]) ? $ip : $_SERVER[&#39;REMOTE_ADDR&#39;]; 
       if($tip=="127.0.0.1"){ //获得本地真实IP
          return $this->get_onlineip();   
       }else{
          return $tip; 
       }
      }
       
      ////获得本地真实IP
      function get_onlineip() {
          $mip = file_get_contents("http://city.ip138.com/city0.asp");
           if($mip){
               preg_match("/\[.*\]/",$mip,$sip);
               $p = array("/\[/","/\]/");
               return preg_replace($p,"",$sip[0]);
           }else{return "获取本地IP失败!";}
       }
       
      ////根据ip获得访客所在地地名
      function Getaddress($ip=&#39;&#39;){
       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 "addree is none";}  
      } 
     }
     $gifo = new get_gust_info();
     echo "你的ip:".$gifo->Getip();
     echo "<br/>所在地:";
     $ipadds = $gifo->Getaddress();
     foreach($ipadds[0] as $value){
         echo "\r\n    ".iconv("utf-8","gbk",$value);    
     }
      
     echo "<br/>浏览器类型:".$gifo->GetBrowser();
     echo "<br/>浏览器语言:".$gifo->GetLang();
     echo "<br/>操作系统:".$gifo->GetOs();
    Copy after login

Related recommendations:

100 commonly used PHP library resources and techniques

7 ways to get the suffix name of PHP files

The above is the detailed content of PHP class library to obtain client IP, geographical information, browser, and real IP. For more information, please follow other related articles on the PHP Chinese website!

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 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!