获取ip,获取客户端浏览器,获取客户端访问操作系统,获取客户端访问设备

/**
     * 获取ip
     */
    public static function getIp()
    {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } else if (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } else if (getenv('REMOTE_ADDR')) {
            $ip = getenv('REMOTE_ADDR');
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $ips = explode(',', $ip);
        if (count($ips) > 1) {
            $ip = $ips[0];
        }
        return $ip;
    }
/**
     * 获取客户端浏览器
     */
    public static function getBrowse()
    {
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
        } else {
            $userAgent = 'unknown';
        }
        $browser = 'other';
        if (preg_match('/MSIE/i', $userAgent)) {
            $browser = 'ie';
        } else if (preg_match('/Firefox/i', $userAgent)) {
            $browser = 'Firefox';
        } else if (preg_match('/Chrome/i', $userAgent)) {
            $browser = 'chrome';
        } else if (preg_match('/Safari/i', $userAgent)) {
            $browser = 'safari';
        } else if (preg_match('/Opera/i', $userAgent)) {
            $browser = 'opera';
        }
        return $browser;
    }
/**
     * 获取客户端访问操作系统
     */
    public static function getOs()
    {
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
        } else {
            $userAgent = 'unknown';
        }

        $os = 'web';
        if (preg_match('/iphone/i', $userAgent)) {
            $os = 'iphone';
        } else if (preg_match('/android/i', $userAgent)) {
            $os = 'android';
        } else if (preg_match('/ipad/i', $userAgent)) {
            $os = 'ipad';
        } else if (preg_match('/win/i', $userAgent)) {
            $os = 'windows';
        } else if (preg_match('/mac/i', $userAgent)) {
            $os = 'mac';
        } else if (preg_match('/linux/i', $userAgent)) {
            $os = 'linux';
        } else if (preg_match('/unix/i', $userAgent)) {
            $os = 'unix';
        } else if (preg_match('/bsd/i', $userAgent)) {
            $os = 'bsd';
        }
        return $os;
    }

    /**
     * 获取客户端访问设备
     */
    public static function getDevice()
    {
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
        } else {
            $userAgent = 'unknown';
        }
        $device = 'pc';
        if (preg_match('/iphone/i', $userAgent)) {
            $device = 'phone';
        } else if (preg_match('/android/i', $userAgent)) {
            $device = 'phone';
        } else if (preg_match('/ipad/i', $userAgent)) {
            $device = 'pad';
        } else if (preg_match('/win/i', $userAgent)) {
            $device = 'pc';
        } else if (preg_match('/mac/i', $userAgent)) {
            $device = 'pc';
        } else if (preg_match('/linux/i', $userAgent)) {
            $device = 'pc';
        } else if (preg_match('/unix/i', $userAgent)) {
            $device = 'pc';
        } else if (preg_match('/bsd/i', $userAgent)) {
            $device = 'pc';
        }
        return $device;
    }
原文地址:https://www.cnblogs.com/-mrl/p/7149144.html