PHP example: Get operating system, IP, geographical location, browser, and other information

  Gets a guest operating system and browser language

    



  //// obtained visitor browser language 
  function GetLang () { 
   IF (empty ($ _ SERVER [ 'the HTTP_ACCEPT_LANGUAGE'])!) { 
    $ Lang = $ _SERVER [ 'the HTTP_ACCEPT_LANGUAGE']; 
    $ lang = substr ($ lang, 0, . 5); 
    IF (the preg_match ( "/ ZH-CN / I", $ lang)) { 
     $ lang = "Simplified Chinese"; 
    } ELSEIF (the preg_match ( "/ ZH / I", $ lang)) { 
     $ lang = " Traditional Chinese "; 
    } the else { 
        $ lang =" English "; 
    } 
    return $ lang; 
    
   } the else {return" failure to obtain browser language ";!} 
  } 
 >? 
<PHP? 
 
   //// acquisition guest operating system 
  function GetOs ( ) { 
   IF (! empty ($ _ SERVER [ 'the HTTP_USER_AGENT'])) { 
    $ _SERVER the OS = $ [ 'the HTTP_USER_AGENT'];
      if (preg_match('/win/i',$OS)) {
     $ The OS = 'the Windows';
    } ELSEIF (the preg_match ( '/ MAC / I', $ the OS)) { 
     $ the OS = 'the MAC'; 
    } ELSEIF (the preg_match ( '/ Linux / I', $ the OS)) { 
     $ the OS = 'the Linux'; 
    } ELSEIF (the preg_match ( '/ UNIX / I', $ the OS)) { 
     $ the OS = 'the Unix'; 
    } ELSEIF (the preg_match ( '/ BSD / I', $ the OS)) { 
     $ the OS = 'the BSD'; 
    } the else { 
     $ = OS 'Other'; 
    } 
          return $ OS;   
   } the else {return "guest operating system to obtain information failed!";}    
  } 
 ?>

  

Access to the user countries, provinces, cities and IP addresses

    

<?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;
}
tangrui_getLocation function (IP = $ '') { 
 empty ($ IP) IP = tangrui_getip && $ (); 
 IF ($ IP == "127.0.0.1") return "local address"; 
 $ API = "HTTP: // int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip "; 
 $ json = @file_get_contents ($ API); // call Sina IP address database 
 $ arr = json_decode ($ json, true ); // parse json 
 $ country = $ arr [ 'country']; // obtained a 
 $ province = $ arr [ 'province ']; // get the provinces 
 $ city = $ arr [ 'city ']; // get city 
 if ((string) $ country == " China") { 
  IF ((String) ($ Province) = (String) $ city!) { 
   $ _location = $ $ Province city;. 
  } the else { 
   $ = $ _location Country . $ City;       
  } 
 } the else { 
  $ $ = Country with _LOCATION;
 }
 return $_location;
}
?>

  

Finally, the following code calls to the appropriate position

IP Address: <? Php echo tangrui_getip ();?>
Location: <php echo tangrui_getLocation ();??>
The type of browser: <php echo GetBrowser ();??>
Browser language: <php echo GetLang ();??>
Operating System: <? Php echo GetOs ();?>
Current time: <? Php echo $ showtime = date ( "Ymd H: i: s");?>

Guess you like

Origin www.cnblogs.com/68xi/p/11596584.html