Bharanikumar
Bharanikumar

Reputation: 25733

Determine client OS in PHP

I have two pages redirection in my index.php. The pages are example_system_os.php and example_mobile_os.php.

How to determine the user's operating system in PHP?

Upvotes: 3

Views: 9957

Answers (2)

Pascal MARTIN
Pascal MARTIN

Reputation: 400912

The get_browser function can be used to extract a couple of information the User-Agent HTTP header sent by the browser.

Amongst those informations, it seems there is some data about the operating system the browser's running on -- i.e. about the client OS.


Quoting the example given on the manual page of get_browser :

Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9


But note that the User-Agent HTTP header is sent by the client, which means :

  • It can be disabled
  • It can be faked (i.e. you can get any kind of garbage in it ^^ )

Upvotes: 6

David Grant
David Grant

Reputation: 14225

You could parse the $_SERVER['HTTP_USER_AGENT'] string for the various platform details, but I wouldn't be confident that this was a deterministic approach, since it can easily be faked.

Upvotes: 4

Related Questions