Results 1 to 4 of 4

Thread: Print Visitor Browser Usage Statistics to Home Page

  1. #1
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Print Visitor Browser Usage Statistics to Home Page

    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...

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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 Code:
    <?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:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Browsers>
        <Firefox>0</Firefox>
        <Safari>0</Safari>
        <IE>0</IE>
        <Chrome>0</Chrome>
        <Opera>0</Opera>
    </Browsers>
    Corrections to my coding/thoughts welcome.

  4. The Following User Says Thank You to bluewalrus For This Useful Post:

    Fighterfox (11-30-2010)

  5. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Can mix the that with this one ( post #5) http://www.dynamicdrive.com/forums/s...ad.php?t=57959

    and should be able to get atleast close to what you need.
    Corrections to my coding/thoughts welcome.

  6. The Following User Says Thank You to bluewalrus For This Useful Post:

    Fighterfox (11-30-2010)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •