View Full Version : Print Visitor Browser Usage Statistics to Home Page
Fighterfox
11-30-2010, 01:40 PM
I would like to detect the browser a visitor is using when they view my home page (body onLoad event for my function). I am only concerned with the major browsers IE, Firefox, Chrome, Safari, and Opera, and not what version or what PC is being used with it. My goal is to put the data into a database so I can sum how many users use what browsers. I would then like to display it on a webpage in a table. I'm having a lot of trouble working my way through this.
I thought about using the get_browser function, but that seems a little bit complicated. Is there something I could use that would be simpler? Thank you very much...
djr33
11-30-2010, 02:27 PM
You posted this in the PHP section, but you mentioned javascript methods like onLoad.
In PHP, you can use $_SERVER['USER_AGENT'] to get the value as a string. It can be incorrect (or even intentionally manipulated), but on average it should be accurate.
You could easily add it as a whole to a database, but if you are trying to get totals for parts of it (rather than the full string) you will need to search it for common 'phrases' like browser names and operating systems. strpos() might give some info and regular expressions would allow more, if you want to do something that complex.
bluewalrus
11-30-2010, 02:43 PM
Here is 1 solution for determining browser by user agent and storing it as an xml file. This doesn't store OS or browser versions. If you want that alterations could be made to it as well.
<?php
//Credit or more info http://php.net/manual/en/function.get-browser.php
function browser_info($agent=null) {
// Declare known browsers to look for
$known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape',
'konqueror', 'gecko');
// Clean up agent and build regex that matches phrases for known browsers
// (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor
// version numbers. E.g. "2.0.0.6" is parsed as simply "2.0"
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';
// Find all phrases (or return empty array if none found)
if (!preg_match_all($pattern, $agent, $matches)) return array();
// Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
// Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
// in the UA). That's usually the most correct.
$i = count($matches['browser'])-1;
return array($matches['browser'][$i] => $matches['version'][$i]);
}
$ua = browser_info();
if ($ua['firefox']) {
$browser = "Firefox";
}
if ($ua['msie']) {
$browser = "IE";
}
if ($ua['opera']) {
$browser = "Opera";
}
if ($ua['safari']) {
$browser = "Safari";
}
$stat_file = "browser.xml";
$stats = file_get_contents($stat_file);
$stats_are = explode ("<" . $browser . ">",$stats);
$stats_are = explode ("</" . $browser . ">", $stats_are[1]);
$value = $stats_are[0] + 1;
$patterns = array();
$patterns[0] = '/<' . "$browser" . '>(.*)' ."<\/$browser>" . '/';
$replacements = array();
$replacements[0] = "<$browser>$value</$browser>";
$output = preg_replace($patterns, $replacements, $stats);
file_put_contents($stat_file, $output);
?>
Browser.xml:
<?xml version="1.0" encoding="utf-8"?>
<Browsers>
<Firefox>0</Firefox>
<Safari>0</Safari>
<IE>0</IE>
<Chrome>0</Chrome>
<Opera>0</Opera>
</Browsers>
bluewalrus
11-30-2010, 09:54 PM
Can mix the that with this one ( post #5) http://www.dynamicdrive.com/forums/showthread.php?t=57959
and should be able to get atleast close to what you need.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.