What is the most reliable way to determine the OS of a visitor to a web site? All other things being equal I prefer an easier to integrate solution. I'm not attempting to gather analytics and I understand there is no completely reliable method. The purpose of this is to subtlely tailor the user experience in ways that do not affect the functionality of the site -- for instance, making a guess at which os version of a cross platform app the user would like to download (I won't hide the other selections, the one matching the user's OS will just become more prominent).
views:
126answers:
4There is no reliable way to do it, and it's not only none of your business, it's also something your web site should pay no attention to.
If this was something your site was meant to know, then there would be a navigator.os property. Note there is not.
On the client side, you can use Javascript to try to detect it:
// This script sets OSName variable as follows:
// "Windows" for all versions of Windows
// "MacOS" for all versions of Macintosh OS
// "Linux" for all versions of Linux
// "UNIX" for all other UNIX flavors
// "Unknown OS" indicates failure to detect the OS
var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
document.write('Your OS: '+OSName);
Sourced from here. Javascript methods are inherently unreliable however. Server side, you can examine some of the HTTP headers in the language of your choice, however, these can also be crafted and are unreliable as well.
In short, there's no 100% reliable method.
Here are examples of Perl code that does OS detection server-side from useragent strings:
- HTTP::BrowserDetect or HTML::ParseBrowser for desktop operating systems
- Mobile::UserAgent for legacy mobile platforms
What is the most reliable way to determine the OS of a visitor to a web site? Ask them on a survey... all else is a crap shoot. Using the UserAgent string you may be able to divine the OS, but as numerous articles will attest - there is no really reliable way to tell.