Home  >  Article  >  Backend Development  >  Example analysis of how to obtain visitor information using PHP

Example analysis of how to obtain visitor information using PHP

黄舟
黄舟Original
2017-09-19 09:37:443324browse

Instance analysis of php implementation of obtaining visitor information

<?php  
header(&#39;Content-Type: text/html; charset=utf-8&#39;);  
  
//获取访客的信息  
class visitorInfo  
{  
    //获取访客ip  
    public function getIp()  
    {  
        $ip=false;  
        if(!emptyempty($_SERVER["HTTP_CLIENT_IP"])){  
            $ip = $_SERVER["HTTP_CLIENT_IP"];  
        }  
        if (!emptyempty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;])) {  
            $ips = explode (", ", $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]);  
            if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }  
            for ($i = 0; $i < count($ips); $i++) {  
                if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {  
                    $ip = $ips[$i];  
                    break;  
                }  
            }  
        }  
        return ($ip ? $ip : $_SERVER[&#39;REMOTE_ADDR&#39;]);  
    }  
   
    //根据ip获取城市、网络运营商等信息  
    public function findCityByIp($ip){  
        $data = file_get_contents(&#39;http://ip.taobao.com/service/getIpInfo.php?ip=&#39;.$ip);  
        return json_decode($data,$assoc=true);  
    }  
   
   //获取用户浏览器类型  
    public function getBrowser(){  
        $agent=$_SERVER["HTTP_USER_AGENT"];  
        if(strpos($agent,&#39;MSIE&#39;)!==false || strpos($agent,&#39;rv:11.0&#39;))  
            return "ie";  
        else if(strpos($agent,&#39;Firefox&#39;)!==false)  
            return "firefox";  
        else if(strpos($agent,&#39;Chrome&#39;)!==false)  
            return "chrome";  
        else if(strpos($agent,&#39;Opera&#39;)!==false)  
            return &#39;opera&#39;;  
        else if((strpos($agent,&#39;Chrome&#39;)==false)&&strpos($agent,&#39;Safari&#39;)!==false)  
            return &#39;safari&#39;;  
        else  
            return &#39;unknown&#39;;  
    }  
   
    //获取网站来源  
    public function getFromPage(){  
        if(isset($_SERVER[&#39;HTTP_REFERER&#39;]))  
            return $_SERVER[&#39;HTTP_REFERER&#39;];  
        else if(strpos($_SERVER[&#39;QUERY_STRING&#39;], &#39;?&#39;))  
            return &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;PHP_SELF&#39;].&#39;?&#39;.$_SERVER[&#39;QUERY_STRING&#39;];  
        else  
            return &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;];  
    }  
}  
  
$v = new visitorInfo();  
echo $v -> getIp(),PHP_EOL;  
var_dump($v->findCityByIp(&#39;101.201.174.210&#39;));  
echo $v->getBrowser(),PHP_EOL;  
echo $v->getFromPage(),PHP_EOL;

Rendering:

Example analysis of how to obtain visitor information using PHP

The above is the detailed content of Example analysis of how to obtain visitor information using PHP. For more information, please follow other related articles on the PHP Chinese website!

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