pTutorial

New way to learn PHP Example

PHP operating system example

How to detect operating system in PHP.

To detect our client operating system use server super global array. Let us see the example of browser detection.

Operating system detection

<?php

$detail = $_SERVER['HTTP_USER_AGENT'];

function os()
 { 
    global $detail ;

    $os1  = "Could not found the oerating System";

    $os_a =  array
	(
			'/windows nt 6.3/i'     =>  'Windows 8.1',
			'/windows nt 6.2/i'     =>  'Windows 8',
			'/windows nt 6.1/i'     =>  'Windows 7',
			'/windows nt 6.0/i'     =>  'Windows Vista',
			'/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
			'/windows nt 5.1/i'     =>  'Windows XP',
			'/windows xp/i'         =>  'Windows XP',
			'/windows nt 5.0/i'     =>  'Windows 2000',
			'/windows me/i'         =>  'Windows ME',
			'/win98/i'              =>  'Windows 98',
			'/win95/i'              =>  'Windows 95',
			'/win16/i'              =>  'Windows 3.11',
			'/macintosh|mac os x/i' =>  'Mac OS X',
			'/mac_powerpc/i'        =>  'Mac OS 9',
			'/linux/i'              =>  'Linux',
			'/ubuntu/i'             =>  'Ubuntu',
			'/iphone/i'             =>  'iPhone',
			'/ipod/i'               =>  'iPod',
			'/ipad/i'               =>  'iPad',
			'/android/i'            =>  'Android',
			'/blackberry/i'         =>  'BlackBerry',
			'/webos/i'              =>  'Mobile'
		);

    foreach ($os_a as $key => $value) { 

        if (preg_match($key, $detail )) {
            $os1    =   $value;
        }

    }   

    return $os1;

}


$os_u = os();

echo  "Operating System:".$os_u;



?>

Explanation

In this example User-Agent information store in the variable $detail (Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page.)

Example: Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:33.0) Gecko/20100101 Firefox/33.0WINNT

Create a function and store all the operating systems into the array variable and one error message of any error.

With the help of foreach loop match the detail variable to the given array and assign the browser at last call browser function in a variable and print this variable.

PHP_OS constant

Operating system using constant

<?php

echo PHP_OS ;


?>

Explanation

In this example directly print the operating system using constant PHP_OS but it's not user-friendly.

Example: WINNT


QUOTES OF THE DAY

To make your children capable of honesty is beginning of education.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER