PHP实例:获取操作系统、IP、地理位置、浏览器、等信息

  获取访客操作系统以及浏览器语言

    

<?php
  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';
    }else {
        $br = 'Other';
    }
    return $br;
   }else{return "获取浏览器信息失败!";} 
  }
 ?> 
 <?php

  ////获得访客浏览器语言
  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 "获取浏览器语言失败!";}
  }
 ?>
<?php
 
   ////获取访客操作系统
  function GetOs(){
   if(!empty($_SERVER['HTTP_USER_AGENT'])){
    $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;  
   }else{return "获取访客操作系统信息失败!";}   
  }
 ?>

  

获取用户所在国家,省份城市以及IP地址

    

<?php
//显示用户所在国家,省份以及城市
function tangrui_getip(){
 if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
   $ip = getenv("HTTP_CLIENT_IP");
  } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
   $ip = getenv("HTTP_X_FORWARDED_FOR");
  } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
   $ip = getenv("REMOTE_ADDR");
  } else  if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
   $ip = $_SERVER['REMOTE_ADDR'];
  } else {
   $ip = "unknown";
  }
 return $ip;
}
function tangrui_getLocation($ip=''){
 empty($ip) && $ip = tangrui_getip();
 if($ip=="127.0.0.1") return "本机地址";
 $api = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip"; 
 $json = @file_get_contents($api);//调用新浪IP地址库 
 $arr = json_decode($json,true);//解析json
 $country = $arr['country']; //取得国家
 $province = $arr['province'];//获取省份 
 $city = $arr['city']; //取得城市
 if((string)$country == "中国"){
  if((string)($province) != (string)$city){
   $_location = $province.$city;
  }else{
   $_location = $country.$city;      
  }
 }else{
  $_location = $country;
 }
 return $_location;
}
?>

  

最后把下面代码调用到适当的位置

IP地址:<?php echo tangrui_getip();?>
所在位置:<?php echo tangrui_getLocation();?>
浏览器类型:<?php echo GetBrowser();?>
浏览器语言:<?php echo GetLang();?>
操作系统:<?php echo GetOs();?>
当前时间:<?php echo $showtime=date("Y-m-d H:i:s");?>
posted @ 2019-09-27 11:05  圆柱模板  阅读(368)  评论(0编辑  收藏  举报