Extract Operating System name from User Agent string using PHP

Identifying a ROBOT form HTTP_USER_AGENT string

Identify a mobile device Operating System from User Agent string

When extracting OS name from $_SERVER["HTTP_USER_AGENT"] string it is important to identify the right OS name and if visitor is not using an OS then identify the ROBOT which is Crawling your website.

How to do it? It can be achieved with a small function which can identify a mobile device or an OS of a computer also it can identify that visitor is a ROBOT

                                    function getOS() {
                                        if (isset($_SERVER["HTTP_USER_AGENT"]) OR ($_SERVER["HTTP_USER_AGENT"] != "")) {
                                            $visitor_user_agent = $_SERVER["HTTP_USER_AGENT"];
                                        } else {
                                            $visitor_user_agent = "Unknown";
                                        // Create list of operating systems with operating system name as array key 
                                        $oses = array(
                                            'Mac OS X(Apple)' => '(iPhone)|(iPad)|(iPod)|(MAC OS X)|(OS X)',
                                            'Apple\'s mobile/tablet' => 'iOS',
                                            'BlackBerry' => 'BlackBerry',
                                            'Android' => 'Android',
                                            'Java Mobile Phones (J2ME)' => '(J2ME/MIDP)|(J2ME)',
                                            'Java Mobile Phones (JME)' => 'JavaME',
                                            'JavaFX Mobile Phones' => 'JavaFX',
                                            'Windows Mobile Phones' => '(WinCE)|(Windows CE)',
                                            'Windows 3.11' => 'Win16',
                                            'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)', 
                                            'Windows 98' => '(Windows 98)|(Win98)',
                                            'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
                                            'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
                                            'Windows 2003' => '(Windows NT 5.2)',
                                            'Windows Vista' => '(Windows NT 6.0)|(Windows Vista)',
                                            'Windows 7' => '(Windows NT 6.1)|(Windows 7)',
                                            'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
                                            'Windows ME' => 'Windows ME',
                                            'Open BSD' => 'OpenBSD',
                                            'Sun OS' => 'SunOS',
                                            'Linux' => '(Linux)|(X11)',
                                            'Macintosh' => '(Mac_PowerPC)|(Macintosh)',
                                            'QNX' => 'QNX',
                                            'BeOS' => 'BeOS',
                                            'OS/2' => 'OS/2',
                                            'ROBOT' => '(Spider)|(Bot)|(Ezooms)|(YandexBot)|(AhrefsBot)|(nuhk)|
                                                        (Ask Jeeves/Teoma)|(Java/1.6.0_04)'
                                        foreach ($oses as $os => $pattern) {
                                            if (eregi($pattern, $visitor_user_agent)) {
                                                return $os;
                                        return 'Unknown';

Copyright © 2011-2014 King of Developers. All Rights Reserved.
Designed By: Abdul Rehman