OS Stats

This is for Brandon, or anyone else that uses this.

How do (or vBulletin) get the stats for users OS? If you could post the php code that would be great, but if it is part of vB, then it might be copyrighted, but still could you describe what it uses.

PS: I mean how the data is receaved, not how the formatting for the stats page is.


You can do it in javascript fairly easily. Just look at the navigator.appVersion object and look for the substring “Win” “Mac” “Linux” or “X11”. I’m sure there are other possible values to check for, but these are the only ones I know of.

You can do it with javascript, yes.

If you want to record/handle it on server-side, however, you can use a few things in PHP.

To get the OS in our ‘stats’ section on chiefdelphi.com … we use a vBulletin hack. Here is the code (yes, its ugly… i didnt write it :slight_smile: ):

if(ereg("Win", getenv("HTTP_USER_AGENT"))) $c_os = "Windows";
elseif((ereg("Mac", getenv("HTTP_USER_AGENT"))) || (ereg("PPC", getenv("HTTP_USER_AGENT")))) $c_os = "Mac";
elseif(ereg("Linux", getenv("HTTP_USER_AGENT"))) $c_os = "Linux";
elseif(ereg("FreeBSD", getenv("HTTP_USER_AGENT"))) $c_os = "FreeBSD";
elseif(ereg("SunOS", getenv("HTTP_USER_AGENT"))) $c_os = "SunOS";
elseif(ereg("IRIX", getenv("HTTP_USER_AGENT"))) $c_os = "IRIX";
elseif(ereg("BeOS", getenv("HTTP_USER_AGENT"))) $c_os = "BeOS";
elseif(ereg("OS/2", getenv("HTTP_USER_AGENT"))) $c_os = "OS/2";
elseif(ereg("AIX", getenv("HTTP_USER_AGENT"))) $c_os = "AIX";
else $c_os = "Other";

Then, $c_os will have the user’s operating system.

And, ugly-coded browser detection:

if((ereg("Nav", getenv("HTTP_USER_AGENT"))) || (ereg("Gold", getenv("HTTP_USER_AGENT"))) || (ereg("X11", getenv("HTTP_USER_AGENT"))) || (ereg("Mozilla", getenv(
"HTTP_USER_AGENT"))) || (ereg("Netscape", getenv("HTTP_USER_AGENT"))) AND (!ereg("MSIE", getenv("HTTP_USER_AGENT")))) $c_browser = "Netscape";
elseif(ereg("MSIE", getenv("HTTP_USER_AGENT"))) $c_browser = "MSIE";
elseif(ereg("Lynx", getenv("HTTP_USER_AGENT"))) $c_browser = "Lynx";
elseif(ereg("Opera", getenv("HTTP_USER_AGENT"))) $c_browser = "Opera";
elseif(ereg("WebTV", getenv("HTTP_USER_AGENT"))) $c_browser = "WebTV";
elseif(ereg("Konqueror", getenv("HTTP_USER_AGENT"))) $c_browser = "Konqueror";
elseif((eregi("bot", getenv("HTTP_USER_AGENT"))) || (ereg("Google", getenv("HTTP_USER_AGENT"))) || (ereg("Slurp", getenv("HTTP_USER_AGENT"))) || (ereg("Scooter", getenv("HTTP_USER_AGENT"))) || (eregi("Spider", getenv("HTTP_USER_AGENT"))) || (eregi("Infoseek", getenv("HTTP_USER_AGENT")))) $c_browser = "Bot";
else $c_browser = "Other";

$c_browser has the browser in it.

And, another way … you would make this script, load it in a browser, and see what happens:

The following example shows how one might list all available information retrieved about the user’s browser.

Example 1. get_browser() example

function list_array ($array) {
    while (list ($key, $value) = each ($array)) {
    $str .= "<b>$key:</b> $value<br />
    return $str;
echo "$HTTP_USER_AGENT<hr />
$browser = get_browser();
echo list_array ((array) $browser);

The output of the above script would look something like this:

Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586)<hr />
<b>browser_name_pattern:</b> Mozilla/4\.5.*<br />
<b>parent:</b> Netscape 4.0<br />
<b>platform:</b> Unknown<br />
<b>majorver:</b> 4<br />
<b>minorver:</b> 5<br />
<b>browser:</b> Netscape<br />
<b>version:</b> 4<br />
<b>frames:</b> 1<br />
<b>tables:</b> 1<br />
<b>cookies:</b> 1<br />
<b>backgroundsounds:</b> <br />
<b>vbscript:</b> <br />
<b>javascript:</b> 1<br />
<b>javaapplets:</b> 1<br />
<b>activexcontrols:</b> <br />
<b>beta:</b> <br />
<b>crawler:</b> <br />
<b>authenticodeupdate:</b> <br />
<b>msn:</b> <br />

This, and discussion, can be found on the PHP manual: http://www.php.net/manual/en/function.get-browser.php


e-mail me at jdmcanally@yahoo.com and i’ll give you our stats script.

it grabs the browser and os and graphs it for you.

i’d show you a demo but i can’t let you into our admin area haha.


Now I see why the stats script puts Konqueror as Netscape. My user agent says Mozilla/5.0 (compatible; Konqueror/3.0.0-10; Linux; X11; i686; , en_US) which means that Konqueror is identified in the first line, it needs an and ! exception like MSIE.

It would also be nice to seperate Gecko based browsers (netscape 6,7, mozilla, galeon, etc.) from netscape 4 browsers. This would be easy to do by adding a Gecko exception to the first line, and then adding another elseif that looks for Gecko

DOH! I just checked the discussion in the PHP manual, and somebody had already done that (at least the Gekco detection)