Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine client OS in PHP

Tags:

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?

like image 769
Bharanikumar Avatar asked Feb 17 '26 22:02

Bharanikumar


2 Answers

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 ^^ )
like image 104
Pascal MARTIN Avatar answered Feb 20 '26 10:02

Pascal MARTIN


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.

like image 45
David Grant Avatar answered Feb 20 '26 11:02

David Grant