Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts

    Stupid problem: counting page views

    Ok... am I going crazy, or does this code sometimes increment by one and sometimes increment by two or three, with each refresh?

    http://dev.glennfergusonarts.com/wtf

    The current views are the first number, and the updated views are the second number. The second number should increment by one, and only one, with each refresh.
    So the second value should be equal to the first value of the next page load after refreshing. I'm about ready to shove a red hot poker into my cornea.

    Edit: Can someone else try this for me? I load the page in IE, and it increments by one. I load the page in Firefox, and it increments by two.
    Whaaat?!?! Am I totally losing it here? Isn't this PHP thing SERVER SIDE?!?!


    PHP Code:
    <?php

    $dbc 
    mysql_connect'localhost''*****''*****' );
    mysql_select_db'*****'$dbc );

    function 
    getViews()
    {
        global 
    $dbc;
        
    $sql 'SELECT `pages`.`viewsTotal` FROM `pages` WHERE `pages`.`id` = 1';
        
    $res mysql_query$sql$dbc );
        
    $row mysql_fetch_assoc$res );
        
    mysql_free_result$res );
        return 
    $row['viewsTotal'];
    }

    function 
    setViews()
    {
        global 
    $dbc;
        
    $sql 'UPDATE `pages` SET `pages`.`viewsTotal` = ( `pages`.`viewsTotal` + 1 ) WHERE `pages`.`id` = 1';
        
    $res mysql_query$sql$dbc );
    }

    echo 
    'Current views: 'getViews(), '<br />';
    echo 
    'Incrementing views.<br />'setViews();
    echo 
    'Updated views: 'getViews(), '<br />';

    mysql_close$dbc );

    ?>
    Last edited by kbluhm; 11-02-2007 at 05:52 AM.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,111
    Thanks
    2
    Thanked 326 Times in 318 Posts
    Based on the URL, I would guess that url rewriting is being used.

    I recall a problem with a missing trailing slash / in the rules, where two requests end up being made to resolve the actual url. I don't recall the specifics, but it goes something like this - the first request to the server determines that the requested url is not to a folder with a default document, the second request resolves to the actual file.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I hadn't even considered that.

    I just loaded the actual file path and the same thing is still happening:
    http://dev.glennfergusonarts.com/wtf/index.php

    Still incrementing by one in IE and by two in FF.

    Maybe I need a nice long night's sleep.

    Here's another stumper:
    http://dev.glennfergusonarts.com/index.html

    Visit that page.
    Click the GO HOME link (index.html) a few times. You will (should) notice the counter in the upper right upping by two (Firefox only??).
    Now, click any other button multiple times. You'll notice the views upping by only one.

    The same centralized code is obviously being used for each page.

    I currently want to rip each and every hair out of my head.

    You've got me thinking CFMaBiSmAd, could there be something in the way different browsers handle mod_rewrite... and I've just never noticed it before? I can't recall this ever being an issue in the past.
    Last edited by kbluhm; 11-02-2007 at 06:24 AM.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,111
    Thanks
    2
    Thanked 326 Times in 318 Posts
    Other than finding what is triggering the multiple requests, I don't know what is actually causing this, but here is a fix -

    Use a session variable to act as a one-shot. If a visitor arrives at a page and a session variable either does not exist or it exists with a value saying they were last on a different page, increment the database counter for that page and set/create the session variable with a value that indicates the current page.

    Then, if they refresh, the session variable will say the last page they were on is the current page and the database increment code will be skipped over.

    If they browse to a different page on the site, the session variable holding the last page will be different than the new current page and the logic will increment the count for that page.

    This logic also works for the case where they browse away from the site but keep the browser open and then come back either to the same last page or a different page.

    If they close the browser/close the session and then come back, it will look like a new view of whatever page they go to.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I'd though about that, but I'm keeping that as a last-ditch option. I'm a freak for minimal, efficient code, memory management, etc. I'm definitely not saying that's the least efficient way to go about it, but if they visit the index page a number of times, the session array will essentially generate a new key with each page load. There are obviously ways to keep the size down, but I'd like to figure out why the heck this is happening as opposed to just applying a band-aid.

    I can't for the life of me figure out why it would happen on the index page, and every other page is counting fine. I think I'll sleep on it and revisit it after work tomorrow.
    Last edited by kbluhm; 11-02-2007 at 06:57 AM.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Gah, I've figured it out... I sent myself the REQUEST_URI in an email directly beneath the UPDATE query.

    In the .htaccess file I was using index.php as the 404 Error Document.

    Firefox was automatically trying to locate /favicon.ico (sometimes twice?). It doesn't exist, so it was loading index.php. Now I just have it skipping /favicon.ico:
    Code:
    RewriteRule ^favicon\.ico$ - [F]
    Problem solved. Oh man, I can sleep easy tonight. There's a good three hours down the drain.

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,111
    Thanks
    2
    Thanked 326 Times in 318 Posts
    I think some of your multiple counts are due to multiple visitors (now that these links have been posted somewhere.) However, I believe the issue is trailing slash on the url (try the one in the first post with a trailing slash on the url so that it does not need to figure out that wtf is a folder instead of a file.)

    why it would happen on the index page
    I think that is significant, as in the web server finding/resolving default documents.

    In searching, I found some articles concerning trailing slashes and the redirect that results when there is not one - http://httpd.apache.org/docs/1.3/mod/mod_dir.html and http://httpd.apache.org/docs/1.3/mis...set-servername
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    The multiple hits were definitely happening before I posted the address. You may have cross-posted as I posted the solution, but it was just a 404 document reloading index.php. I had no idea Firefox would automatically make a request to favicon.ico. All example are working fine now because I have absolutely cut-off favicon.ico.

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    favicon.ico's can mess you up like that.

    I once had IE come up with Page not Found for a URL that I knew worked.

    Looked at the URL and surprise, it was showing the favicon.ico that it attempted to load. Which obviously didn't exist.

    I believe IE's implementation of favicon.ico has been criticized before.

    But I'm curious, how come a request for a resource file is being processed by your PHP?


  •  

    Posting Permissions

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