Firefox double counter problem

    Date: 05/01/07 (PHP Community)    Keywords: browser, database

    Hello again, everyone.

    I'm implementing a very simple 'self-configuring' counter script that essentially creates a counter for every page it's used on with the option to update the counter or not and to return or display the page's counter. It interfaces to a simple, one-table database with only two columns.


    define("NOTSET",-1024);
    $server = "server";
    $user = "user";
    $password = "password";
    $db = "db";
    $link = mysql_connect($server,$user,$password) or die("Cannot establish connection with counter server");
    mysql_select_db($db) or die("Cannot select database");

    //returns the CID (table col) hash according to $id
    //  (id should be the SCRIPT_FILENAME of the requesting page)
    function counterHash($id) {
      $md5 = md5($id); // md5 is relatively surjective
      return substr($md5,0,10);
      }
    function getCount($hash) {
      global $link;
      $safe = mysql_real_escape_string($hash);
      $query = "SELECT `count` FROM `counters` WHERE `CID`='".$safe."'";
      $result = mysql_query($query);
      if ( ! is_resource($result) ) { print("notset!"); return NOTSET; }
      $row = mysql_fetch_array($result,MYSQL_ASSOC);
      return $row['count'];
      }

    // If there isn't a row for $hash in the db, then one is
    // created and false is returned.
    // Otherwise, returns true.
    function createRowForHashIfNotExists($hash) {
      global $link;
      $safe = mysql_real_escape_string($hash);
      $q = "SELECT `count` FROM `counters` WHERE `CID`='$safe'";
      $result = mysql_query($q) or die("Cannot get status because ".mysql_error() );
      $n = mysql_num_rows($result);
      if ( $n === 0 ) { // no rows for our hash
        $q = "INSERT INTO `counters` (`CID`,`count`) VALUES('$safe','0')";
        mysql_query($q) or die("cannot create new counter because ".mysql_error() );
        return false;
        }
      }

    //sets the `count` col of $hash's row to $count
    function setCount($hash,$count) {
      global $link;
      createRowForHashIfNotExists($hash);
      $safehash = mysql_real_escape_string($hash);
      $safecount = (int)mysql_real_escape_string( (int)$count );
      if ( ! is_int($safecount) ) { die('setcount(hash,INTEGER)'); }
      $query = "UPDATE `counters` SET `count` ='".$safecount."' WHERE `CID` = '".$hash."' LIMIT 1";
      mysql_query($query) or die("Cannot update counter because " . mysql_error() );
      }
      
    $id = $_SERVER['SCRIPT_FILENAME'];
    $hash = counterHash($id);
    createRowForHashIfNotExists($hash);
    $visitorCounter = getCount($hash);

    function visitorCounter( $update=true, $show=true ) {
      global $link, $visitorCounter, $hash;
      if ( $update ) {
        setCount($hash,++$visitorCounter);
        }
      if ( $show ) {
        echo '
    '.$visitorCounter.' Visitors
    '
    ."\n";
        } else { // don't show so return
          return $visitorCounter;
          }
        return true;
      }

    ?>

    Everything works just fine in Safari (i.e. the page is visited and the counter increments only once), but in Firefox, the counter increments twice for no apparent reason. I figured this out only after wasting like three hours tracing through the program and reconstructing everything from scratch.

    Is anybody familiar with what might be causing this? Is there a glaring problem with my code? Or, perhaps, most importantly, do you happen to see a fix or workaround? I could just do a browser detection and then not increment if firefox, but that's really more like a bad hack than an effective workaround...

    Thanks in advance!

    Source: http://community.livejournal.com/php/563403.html

« text areas || Coding Practice »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home