Detecting OS by Using PHP

Wednesday, March 9, 2011 at 8:08 PM
well, while I was working on my project, I run into a problem of detecting OS by using PHP. After a bit of googling, I found a solution from Geek Pedia so big thanks to the guy who posted the solution but when I tried it on my comp, I have to deal with  another problem, since I have installed PHP 5.3 and the function eregi seems to be outdated. I have to come up with another solution myself. I just modified his code a little bit to work on my OS. So here it is, copy and paste it and test it:

function checkOS(){
    $OSList = array
    (
        // Match user agent string with operating systems
        'Windows 3.11' => '/Win16/i',
        'Windows 95' => '/(Windows 95)|(Win95)|(Windows_95)/i',
        'Windows 98' => '/(Windows 98)|(Win98)/i',
        'Windows 2000' => '/(Windows NT 5.0)|(Windows 2000)/i',
        'Windows XP' => '/(Windows NT 5.1)|(Windows XP)/i',
        'Windows Server 2003' => '/(Windows NT 5.2)/i',
        'Windows Vista' => '/(Windows NT 6.0)/i',
        'Windows 7' => '/(Windows NT 7.0)/',
        'Windows NT 4.0' => '/(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)/i',
        'Windows ME' => '/Windows ME/i',
        'Open BSD' => '/OpenBSD/i',
        'Sun OS' => '/SunOS/i',
        'Linux' => '/(Linux)|(X11)/i',
        'Mac OS' => '/(Mac_PowerPC)|(Macintosh)/i',
        'QNX' => '/QNX/i',
        'BeOS' => '/BeOS/i',
        'OS/2' => '/OS/2/i',
        'Search Bot'=>'/(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)/i'
    );

    //     Loop through the array of user agents and matching operating systems
    foreach($OSList as $CurrOS=>$Match)
    {
            // Find a match
            if (preg_match($Match, $_SERVER['HTTP_USER_AGENT']))
            {
                    // We found the correct match
                    break;
            }
    }
    //echo "You are using ".$CurrOS;
    return $CurrOS;
}

Hope this help

0 comments

Post a Comment