Home > Backend Development > PHP Tutorial > How to use php to obtain visitor IP and regional location information, as well as share examples of browser and source page

How to use php to obtain visitor IP and regional location information, as well as share examples of browser and source page

黄舟
Release: 2023-03-14 14:30:01
Original
2032 people have browsed it

How to use php to obtain visitor IP and regional location information and share examples of browser and source pages

<?php
//这个类似用来获取访客信息的
//方便统计
class visitorInfo
{
    //获取访客ip
    public function getIp()
    {
        $ip=false;
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty($_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;)) //ie11判断
            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(){
        return $_SERVER[&#39;HTTP_REFERER&#39;];
    }
 
}
Copy after login

The above is the detailed content of How to use php to obtain visitor IP and regional location information, as well as share examples of browser and source page. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template