3

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bharanikumar
  • 25,457
  • 50
  • 131
  • 201
  • Maybe a usefull info - you can test what browser is used based on the way JavaScript is executed. And there is a tiny possibility that this method is applicable for the OS, due to minor differences in the browser version on differnt OS. – Bakudan Mar 11 '11 at 21:36

2 Answers2

6

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 ^^ )
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • i run this get browser function , i find one array that is ismobiledevice , ut in my system it is just showing empty array... – Bharanikumar Feb 17 '10 at 12:45
  • i thing in my mobile device it showing the 1 shall i use , something like when this array is one then redirect page to example_mobile_os.php – Bharanikumar Feb 17 '10 at 12:48
4

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.

David Grant
  • 13,929
  • 3
  • 57
  • 63