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

x33g5p2x  于2022-03-07 转载在 其他  
字(1.9k)|赞(0)|评价(0)|浏览(336)

代码如下:

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/>";
    }
}

运行截图如下:

相关文章