hi,欢迎访问本站!
当前位置: 首页编程开发正文

php怎么获取访客操作系统的类型

墨初 编程开发 298阅读

php想要获取访问者操作系统的类型就需要借助浏览器的UserAgent信息,而UserAgent信息不仅仅包含了访问者使用的操作系统类型,也包括了使用的浏览器名称,版本号,以及使用的IP地址等信息。下面这骗博文只简单的介绍一下,利用php脚本如何在UserAgent信息中分析出访问者的操作系统类型,以及版本号。

php 获取访问者的操作系统信息

下面是网上收集了的php获取访客操作系统信息的函数,各位可以参考一下。

例1:

/**
 * @name  php获取用户操作系统信息
 * 
 * @return string 用户的操作系统信息
 * @author http://73so.com
 */
function get_user_os() 
{
    if (isset($_SERVER["HTTP_USER_AGENT"])) {
        $user_agent = strtolower($_SERVER["HTTP_USER_AGENT"]);
    } else {
        return null;
    }
    
    $user_os = null;
    if (strpos($user_agent, 'windows nt 5.0')) {
        $user_os = 'Windows 2000';
    } elseif (strpos($user_agent, 'windows nt 9')) {
        $user_os = 'Windows 9X';
    } elseif (strpos($user_agent, 'windows nt 5.1')) {
        $user_os = 'Windows XP';
    } elseif (strpos($user_agent, 'windows nt 5.2')) {
        $user_os = 'Windows 2003';
    } elseif (strpos($user_agent, 'windows nt 6.0')) {
        $user_os = 'Windows Vista';
    } elseif (strpos($user_agent, 'windows nt 6.1')) {
        $user_os = 'Windows 7';
    } elseif (strpos($user_agent, 'windows nt 6.2')) {
        $user_os = 'Windows 8';
    } elseif (strpos($user_agent, 'windows nt 6.3')) {
        $user_os = 'Windows 8.1';
    } elseif (strpos($user_agent, 'windows nt 10')) {
        $user_os = 'Windows 10';
    } elseif (strpos($user_agent, 'windows phone')) {
        $user_os = 'Windows Phone';
    } elseif (strpos($user_agent, 'android')) {
        $user_os = 'Android';
    } elseif (strpos($user_agent, 'iphone')) {
        $user_os = 'iPhone';
    } elseif (strpos($user_agent, 'ipad')) {
        $user_os = 'iPad';
    } elseif (strpos($user_agent, 'mac')) {
        $user_os = 'Mac';
    } elseif (strpos($user_agent, 'sunos')) {
        $user_os = 'Sun OS';
    } elseif (strpos($user_agent, 'bsd')) {
        $user_os = 'BSD';
    } elseif (strpos($user_agent, 'ubuntu')) {
        $user_os = 'Ubuntu';
    } elseif (strpos($user_agent, 'linux')) {
        $user_os = 'Linux';
    } elseif (strpos($user_agent, 'unix')) {
        $user_os = 'Unix';
    } else {
        $user_os = 'Other';
    }
    return $user_os;
}

例2:

/**
 * @name  php获取用户操作系统信息
 * 
 * @return string 用户的操作系统信息
 * @author http://73so.com
 */
function get_user_os() 
{
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $os = '';
    if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent)){
        $os = 'Windows 10';
    }else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent)){
        $os = 'Windows 8';
    }else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent)){
        $os = 'Windows 7';
    }else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent)){
        $os = 'Windows Vista';
    }else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent)){
        $os = 'Windows XP';
    }else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90')){
        $os = 'Windows ME';
    }else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent)){
        $os = 'Windows 98';
    }else if (preg_match('/win/i', $agent) && strpos($agent, '95')){
        $os = 'Windows 95';
    }else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent)){
        $os = 'Windows 2000';
    }else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent)){
        $os = 'Windows NT';
    }else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent)){
        $os = 'Windows 32';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android 12/i', $agent)){
        $os = 'Android 12';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android 11/i', $agent)){
        $os = 'Android 11';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android 10/i', $agent)){
        $os = 'Android 10';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android q/i', $agent)){
        $os = 'Android Q';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android 9/i', $agent)){
        $os = 'Android 9';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android 7/i', $agent)){
        $os = 'Android 7';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android 6/i', $agent)){
        $os = 'Android 6';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android 5/i', $agent)){
        $os = 'Android 5';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android 4/i', $agent)){
        $os = 'Android 4';
    }else if (preg_match('/linux/i', $agent) && preg_match('/android/i', $agent)){
        $os = 'Android';
    }else if (preg_match('/harmony/i', $agent) && preg_match('/harmony/i', $agent)){
        $os = 'Harmony OS 鸿蒙';
    }else if (preg_match('/linux/i', $agent)){
        $os = 'Linux';
    }else if (preg_match('/unix/i', $agent)){
        $os = 'Unix';
    }else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent)){
        $os = 'SunOS';
    }else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent)){
        $os = 'IBM OS/2';
    }else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent)){
        $os = 'Macintosh';
    }else if (preg_match('/PowerPC/i', $agent)){
        $os = 'PowerPC';
    }else if (preg_match('/AIX/i', $agent)){
        $os = 'AIX';
    }else if (preg_match('/HPUX/i', $agent)){
        $os = 'HPUX';
    }else if (preg_match('/NetBSD/i', $agent)){
        $os = 'NetBSD';
    }else if (preg_match('/BSD/i', $agent)){
        $os = 'BSD';
    }else if (preg_match('/OSF1/i', $agent)){
        $os = 'OSF1';
    }else if (preg_match('/IRIX/i', $agent)){
        $os = 'IRIX';
    }else if (preg_match('/FreeBSD/i', $agent)){
        $os = 'FreeBSD';
    }else if (preg_match('/teleport/i', $agent)){
        $os = 'teleport';
    }else if (preg_match('/flashget/i', $agent)){
        $os = 'flashget';
    }else if (preg_match('/webzip/i', $agent)){
        $os = 'webzip';
    }else if (preg_match('/offline/i', $agent)){
        $os = 'offline';
    }
    return $os;
}

例3:

/**
 * @name  php获取用户操作系统信息
 * 
 * @return string 用户的操作系统信息
 * @author http://73so.com
 */
function get_os() {
    if (!empty($_SERVER['HTTP_USER_AGENT'])) {
        $os = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/win/i', $os)) {
            $os = 'Windows';
        } else if (preg_match('/mac/i', $os)) {
            $os = 'MAC';
        } else if (preg_match('/linux/i', $os)) {
            $os = 'Linux';
        } else if (preg_match('/unix/i', $os)) {
            $os = 'Unix';
        } else if (preg_match('/bsd/i', $os)) {
            $os = 'BSD';
        } else {
            $os = 'Other';
        }
        return $os;
    } else {
        return 'unknow';
    }
}
声明:无特别说明,转载请标明本文来源!
相关推荐