Home  >  Article  >  Backend Development  >  Two methods for obtaining browser information and client location in PHP_PHP Tutorial

Two methods for obtaining browser information and client location in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:57775browse

1. Get browser information, PHP class to obtain guest operating system: windows, mac, linux, unix, bsd, other, and visitor IP address and other information

Copy code The code is as follows:

/**
* Classes for obtaining visitor information: language, browser, operating system, ip, geographical location, isp.
* Usage:
* $obj = new guest_info;
* $obj->getlang(); //Get guest language: simplified Chinese, traditional Chinese, english.
* $obj->getbrowser(); //Get the visitor's browser: msie, firefox, chrome, safari, opera, other.
* $obj->getos(); //Get the guest operating system: windows, mac, linux, unix, bsd, other.
* $obj->getip(); //Get the visitor’s IP address.
* $obj->getadd(); //Get the visitor’s geographical location and use baidu hidden interface.
* $obj->getisp(); //Get the visitor isp and use baidu to hide the interface.
*/
class guest_info{
function getlang() {
$lang = substr($_server['http_accept_language'], 0, 4);
//Use substr() to intercept String, starting from position 0, 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 = 'english';                                                     
if (preg_match('/msie/i',$browser)) {
             $browser = 'msie';                                                             🎜>            $browser = 'firefox';                                               
elseif (preg_match('/safari/i',$browser)) {
$browser = 'safari';
elseif (preg_match('/opera/i',$browser)) {
                $browser = 'opera';                                               > }  
  function getos() {
$os = $_server['http_user_agent'];
if (preg_match('/win/i',$os)) {
$os = 'windows';
}
elseif (preg_match('/mac/i',$os)) {
                                                                             🎜>             $os = 'linux';                                                   🎜> elseif (preg_match('/bsd/i',$os)) {
                                                       🎜 >           return $os; Then empty() returns false.
           $ip = explode(',',$_server['http_client_ip']);                                     (',',$_server['http_x_forwarded_for']);                                   ['remote_addr']);
      }                                                                  ​  
}

$obj = new guest_info;
echo $obj->getlang(); //Get guest language: simplified Chinese, traditional Chinese, english.
echo $obj->getbrowser(); //Get the visitor's browser: msie, firefox, chrome, safari, opera, other.
echo $obj->getos(); //Get the guest operating system: windows, mac, linux, unix, bsd, other.
echo $obj->getip(); //Get the visitor’s IP address.
?>





2. PHP uses Tencent IP sharing plan to obtain IP location





Copy code
The code is as follows:


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, 10); curl_setopt($ch, curlopt_returntransfer, true); // Get data and return it $result = curl_exec($ch); $ result = mb_convert_encoding($result, "utf-8", "gb2312"); // Encoding conversion, otherwise garbled code
curl_close($ch);
preg_match("@(.*)< ;/span>

@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.
?>




3. PHP uses Sina IP query interface to obtain IP geographical location





Copy code
The code is as follows:

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');                                                                                                                                                         ); $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");
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/760292.htmlTechArticle1. Get browser information and guest operating system: windows, mac, linux, unix, bsd, other, As well as the PHP class copy code of the visitor's IP address and other information is as follows: ?php /** * Get...

Statement:
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