Sabai
Sabai

Reputation: 1599

Is it possible to detect what operating system a user is coming from using PHP? (mac or windows)

Let's say for example I wanted to echo "You are using Windows!" or "You are using Macintosh!", depending on the users OS. Is this possible?

Upvotes: 4

Views: 2636

Answers (5)

Bill Karwin
Bill Karwin

Reputation: 562330

Try the get_browser() function that's built into PHP.

$browser = get_browser(null, true);
echo "Platform: " . $browser["platform"] . "\n"; 

Upvotes: 11

Rupert Madden-Abbott
Rupert Madden-Abbott

Reputation: 13278

Yes it is possible.

You want to use $_SERVER['HTTP_USER_AGENT'] which holds information about the user's operating system and browser.

You can use this resource to look up user agent strings (which are held in that variable).

However, it is possible for browsers to spoof this information so you can't assume that this is reliable.

Upvotes: 1

ZJR
ZJR

Reputation: 9572

By analyzing $_SERVER['HTTP_USER_AGENT'] it's possible to tell what system (and browser) the user is claiming to use.

It's easily spoofable, though.

Upvotes: 14

Steav
Steav

Reputation: 1486

http://phpcode.mypapit.net/detect-ip-location-operating-system-and-browser-using-php-detector-library/46/

The library is handy for for creating web application which serve content depending on users location and type of operating system/browser that he use or for creating web application that collect web surfers statistical data.

code example:

  require('detector.php');
  $dip = &new Detector($_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"]);

  echo "$dip->town";

  echo "$dip->state, $dip->ccode,$dip->town, ($dip->ipaddress) ";

  echo "using : $dip->browser $dip->browser_version on $dip->os $dip->os_version";

Upvotes: 1

Quentin
Quentin

Reputation: 943559

You can look at the user agent string, which often includes information about the OS.

Note, however, that there are operating systems other than Windows and OS X.

Upvotes: 6

Related Questions