Define User Operating System

Discussion in 'Programming' started by iMatt, Dec 8, 2007.

Thread Status:
Not open for further replies.
  1. iMatt New Member

    Member Since:
    Nov 11, 2007
    Message Count:
    50
    Likes Received:
    0
    Occupation:
    Student
    Location:
    Manchester, UK
    This function will return the users operating system, or 'Unknown OS' if the code doesn't know.

    PHP:
    <?php

    function define_os () {

        global $_SERVER;
        define("USERAGENT", $_SERVER['HTTP_USER_AGENT']);

        $operatingSystems = array(

                                    'Windows Vista' => '/windows nt 6.0/i',
                                    'Windows Server 2003' => '/windows nt 5.2/i',
                                    'Windows XP' => '/windows nt 5.1/i',
                                    'Windows 2000 SP1' => '/windows nt 5.01',
                                    'Windows 2000' => '/windows nt 5.0/i',
                                    'Windows NT 4.0' => '/windows nt 4.0/i',
                                    'Windows NT' => '/windows nt/i',
                                    'Windows ME' => '/win 9x 4.90/i',
                                    'Windows 98' => '/windows 98/i',
                                    'Windows 95' => '/windows 95/i',
                                    'Windows 3.x' => '/windows 3.1/i',
                                    'Windows XP (IE7 Beta)' => '/win32/i',
                                    'Linux' => '/linux/i',
                                    'FreeBSD' => '/freebsd/i',
                                    'NetBSD' => '/netbsd/i',
                                    'OpenBSD' => '/openbsd/i',
                                    'Mac (68000)' => '/mac_68000/i',
                                    'Mac (PPC)' => '/mac_ppc/i',
                                    'Mac PowerPC' => '/mac_powerpc/i',
                                    'PPC Mac OS X' => '/ppc mac os x/i',
                                    'SunOS' => '/sunos/i',
                                    'Windows Vista Media Center' => '/media center pc 5.0/i',
                                    'Windows Media Center' => '/media center pc/i',
                                    'Windows Media Center' => '/mediacenter/i',
                                    'Windows ME' => '/win 9x 4.90/i',
                                    'Windows 98' => '/win98/i',
                                    'Windows 95' => '/win95/i',

                                  );


        $array['operating_system'] = 'Unknown OS';

        foreach ($operatingSystems as $systemName => $pattern) {

            if(preg_match($pattern, USERAGENT)) {

                return $systemName;

            }

        }

    }

    // Browser name with a icon

    echo '<b>Your Operating System:</b> <i>' . define_os() . '</i>';

    ?>
    I am working on the arrays of more operating systems to create a more detailed list of operating systems this code can show. I will update this topic soon.

    EDIT: More detailed list here, this is if you want to a wider variety of OS's:

    PHP:
    <?php

    function define_os () {

    global $_SERVER;
    define("USERAGENT", $_SERVER['HTTP_USER_AGENT']);

    $operatingSystems = array(

    'Windows Vista' => '/windows nt 6.0/i',
    'Windows Vista Media Center' => '/media center pc 5.0/i',
    'Windows Media Center' => '/media center pc/i',
    'Windows Media Center' => '/mediacenter/i',
    'Windows Server 2003' => '/windows nt 5.2/i',
    'Windows XP' => '/windows nt 5.1/i',
    'Windows 2000 SP1' => '/windows nt 5.01',
    'Windows 2000' => '/windows nt 5.0/i',
    'Windows 2000 (Opera)' => '/windows 2000/i',
    'Windows NT 4.0' => '/windows nt 4.0/i',
    'Windows NT' => '/windows nt/i',
    'Windows ME' => '/win 9x 4.90/i',
    'Windows 98' => '/windows 98/i',
    'Windows 95' => '/windows 95/i',
    'Windows 3.x' => '/windows 3.1/i',
    'Windows CE/Mobile' => '/windows ce/i',
    'Windows CE 4.21' => '/windows ce 4.21/i',
    'Windows XP (IE7 Beta)' => '/win32/i',
    'Windows ME' => '/win 9x 4.90/i',
    'Windows 98' => '/win98/i',
    'Windows 95' => '/win95/i',
    'Windows 3.11' => '/win3.11/i',
    'Windows NT 3.51' => '/winnt3.51/i',
    'Windows NT 4.0' => '/winnt4.0/i',
    'Linux' => '/linux/i',
    'Ubuntu Feisty Fawn' => '/\(Ubuntu-feisty\)/i',
    'FreeBSD' => '/freebsd/i',
    'NetBSD' => '/netbsd/i',
    'OpenBSD' => '/openbsd/i',
    'Mac (68000)' => '/mac_68000/i',
    'Mac (PPC)' => '/mac_ppc/i',
    'Mac PowerPC' => '/mac_powerpc/i',
    'PPC Mac OS X' => '/ppc mac os x/i',
    'PPC Mac OS X Mach-O' => '/ppc mac os X mach-O/i',
    'Mac OS X (Intel)' => '/intel mac os x/i',
    'SunOS' => '/sunos/i',
    'Symbian OS (Mobile)' => '/symbian os/i',
    'Nitro (Nintendo DS)' => '/nitro/i',
    'Playstation Portable' => '/psp/i',
    'Playstation 3' => '/playstation 3/i',
    'Windows 64-bit' => '/win64/i',
    'Intel Intanium Processor' => '/ia64/i',
    '64-bit AMD Athlon Processor' => '/amd64/i',
    'x64 Processor' => '/x64/i',
    'MSIE Crawler' => '/msiecrawler/i',
    'SunOS (IE)' => '/x11/i',
    'Pocket PC' => '/ppc/i',
    'Smart Phone' => '/smartphone/i',
    'Motorola Phone' => '/motorola/i',
    'Nokia Phone' => '/nokia/i'

    );


    $array['operating_system'] = 'Unknown OS';

    foreach ($operatingSystems as $systemName => $pattern) {

    if(preg_match($pattern, USERAGENT)) {

    $return = $systemName;

    break;

    }

    }

    return $return;

    }

    // Browser name with a icon

    echo '<b>Your Operating System:</b> <i>' . define_os() . '</i>';

    ?>
  2. Hayze New Member

    Member Since:
    Sep 6, 2007
    Message Count:
    949
    Likes Received:
    0
    Occupation:
    Un Occupied.
    Location:
    To your left... no your other left.
    WOW, I never knew you could do this with PHP, you are amazing at PHP!
  3. iMatt New Member

    Member Since:
    Nov 11, 2007
    Message Count:
    50
    Likes Received:
    0
    Occupation:
    Student
    Location:
    Manchester, UK
    Why thanks Bill. I do try to help people who aren't so wise in PHP. And no, I am not talking about you as Pandemix has told me of your skills :p
Thread Status:
Not open for further replies.

Share This Page