PHP access to browser information, guest language, guest operating system, guest IP address and visitor location

Source: Internet
Author: User
Tags empty explode regular expression linux

PHP gets the browser information. Get the guest language. Gets the guest operating system. Gets the guest IP address. Get a visitor's geographic location.

PHP access to browser information, access to the guest language: Simplified Chinese, Chinese, English. Get guest OS: Windows, MAC, Linux, Unix, BSD, other. Gets the guest IP address. Access to the location of visitors, the use of Sina Tencent interface.

<?php/** * Access to the class of guest information: language, browser, operating system, IP, geographic location, ISP. 
 * Use: * $obj = new Guest_info;     * $obj->getlang (); 
 Get guest language: Simplified Chinese, Chinese, English.      * $obj->getbrowser (); 
 Get guest browsers: MSIE, Firefox, Chrome, Safari, Opera, and other.           * $obj->getos (); 
 Get guest OS: Windows, MAC, Linux, Unix, BSD, other.           * $obj->getip (); 
 Gets the guest IP address.          * $obj->getadd (); 
 Get the visitor's geographic location and use the Baidu to hide the interface.          * $obj->getisp (); 
 Get the guest ISP and use Baidu to hide the interface.  
        * * Class guest_info{function Getlang () {$Lang = substr ($_server[' http_accept_language '), 0, 4);  
            Intercepts a string using substr (), starting at 0 bits, intercepting 4 characters if (Preg_match ('/zh-c/i ', $Lang)) {//preg_match () regular expression matching function  
        $Lang = ' Simplified Chinese ';  
        } elseif (Preg_match ('/zh/i ', $Lang)) {$Lang = ' Chinese ';  
        else {$Lang = ' 中文版 ';  
    return $Lang;  
       function Getbrowser () { $Browser = $_server[' http_user_agent '];  
        if (Preg_match ('/msie/i ', $Browser)) {$Browser = ' msie ';  
        } elseif (Preg_match ('/firefox/i ', $Browser)) {$Browser = ' Firefox ';  
        } elseif (Preg_match ('/chrome/i ', $Browser)) {$Browser = ' Chrome ';  
        } elseif (Preg_match ('/safari/i ', $Browser)) {$Browser = ' Safari ';  
        } elseif (Preg_match ('/opera/i ', $Browser)) {$Browser = ' Opera ';  
        else {$Browser = ' other ';  
    return $Browser;  
        function Getos () {$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;   
            function GetIP () {if (!empty ($_server[' http_client_ip ')) {//If the variable is non-null or Non-zero, empty () returns FALSE.  
        $IP = Explode (', ', $_server[' http_client_ip ')); } elseif (!empty ($_server[' http_x_forwarded_for ')) {$IP = explode (', ', $_server[' http_x_forwarded  
        _for ']);  
        } elseif (!empty ($_server[' remote_addr ')) {$IP = explode (', ', $_server[' remote_addr ']);  
        else {$IP [0] = ' None ';  
    return $IP [0];  
}} $obj = new Guest_info;     echo $obj->getlang ();  
Get guest language: Simplified Chinese, Chinese, English.      echo $obj->getbrowser ();  
Get guest browsers: MSIE, Firefox, Chrome, Safari, Opera, and other. echo $obj->getos ();           Get guest OS: Windows, MAC, Linux, Unix, BSD, other.           echo $obj->getip (); Gets the guest IP address.?>

One, PHP use Tencent IP sharing program to obtain IP location

<?php  
function Getiploc_qq ($queryIP) {      
$url = ' http://ip.qq.com/cgi-bin/searchip?searchip1= '. $queryIP;      
$ch = Curl_init ($url);      
curl_setopt ($ch, curlopt_encoding, ' gb2312 ');    
curl_setopt ($ch, Curlopt_timeout, ten);     
curl_setopt ($ch, Curlopt_returntransfer, true); Get Data back    
$result = curl_exec ($ch);      
$result = mb_convert_encoding ($result, "Utf-8", "gb2312"); Code conversion, otherwise garbled   
curl_close ($ch);     
Preg_match ("@<span> (. *) </span></p> @iU", $result, $ipArray);      
$loc = $ipArray [1];      
return $loc;  
 }   
Use  
echo    getiploc_qq ("183.37.209.57");//You can get the address location of the IP address.     
?>

Second, php use Sina IP query interface to obtain IP location

<?php   
function Getiploc_sina ($queryIP) {      
$url = ' http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format=json&ip= '. $queryIP;      
$ch = Curl_init ($url);       
curl_setopt ($ch, curlopt_encoding, ' UTF8 ');       
curl_setopt ($ch, Curlopt_timeout, 5);     
curl_setopt ($ch, Curlopt_returntransfer, true); Get Data back    
$location = curl_exec ($ch);      
$location = Json_decode ($location);      
Curl_close ($ch);           
$loc = "";     
if ($location ===false) return "";       
if (Empty ($location->desc)) {      
$loc = $location->province. $location->city. $location->district.$ location->isp;    
} else{         $loc = $location->desc;      
}      
return $loc;  
}  
Echo Getiploc_sina ("183.37.209.57");  
? >

Click this address to download it: http://download.csdn.net/detail/u011986449/6871205

The effect of the following figure:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.