PHP笔记-获取客户端IP及平台及浏览器

Posted IT1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP笔记-获取客户端IP及平台及浏览器相关的知识,希望对你有一定的参考价值。

代码如下:

class CookieTool

	......
	......
	......

    protected function getIPAddress(): string

        $ipaddress = "";

        if (isset($_SERVER['HTTP_CLIENT_IP']))
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        else if(isset($_SERVER['HTTP_X_FORWARDED']))
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
        else if(isset($_SERVER['HTTP_FORWARDED']))
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
        else if(isset($_SERVER['REMOTE_ADDR']))
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        else
            $ipaddress = 'UNKNOWN';

        return $ipaddress;
    

    protected function getBrowser($agent): string

        $browserAgent = "";
        if(strstr($agent, 'MSIE')) 

            $browserAgent="Internet Explorer";
        
        else if(strstr($agent, 'Opera')) 

            $browserAgent="Opera";
        
        else if(strstr($agent, 'Firefox')) 

            $browserAgent="Firefox";
        
        else if(strstr($agent, 'Chrome')) 

            $browserAgent = "Chrome";
        
        else if(strstr($agent, 'Safari')) 

            $browserAgent = "Safari";
        
        else

            $browserAgent = "unknown";
        

        return $browserAgent;
    

    protected function getPlatform($agent): string

        $agent = strtolower($agent);
        $platform = "";
        if(strstr($agent, 'win')) 

            $platform="windows";
        
        else if(strstr($agent, 'linux')) 

            $platform = "linux";
        
        else

            $platform = "unknown";
        

        return $platform;
    

    protected function getMacAddress(): string

        $MAC = exec('getmac');
        print_r($MAC);
        $MAC = strtok($MAC, ' ');
        return $MAC;
    

    public function printCookieArray()

        global $cookieAndSessionArray;
        print_r($cookieAndSessionArray);
    


    public function XXXXXX()


        $ipAddress = $this->getIPAddress();
        $browserAgent = $this->getBrowser($_SERVER['HTTP_USER_AGENT']);
        $platform = $this->getPlatform($_SERVER['HTTP_USER_AGENT']);
        echo $ipAddress . "<br/>";
        echo $browserAgent . "<br/>";
        echo $platform . "<br/>";
    

运行截图如下:

 

以上是关于PHP笔记-获取客户端IP及平台及浏览器的主要内容,如果未能解决你的问题,请参考以下文章

利用PHP获取访客IP地区位置浏览器及来源页面等信息

PHP代码获取客户端IP地址经纬度及所在城市

JS和C#.NET获取客户端IP

博客园OAuth2.0 授权及用户登录

php 如何获取服务器的信息

HTTP协议学习笔记