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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Stop counter refresh

    OK, my first post here...very newbie at PHP – so, please be Gentle…
    Trying to see if anyone could help with the following problem, I have searched, but really did not find anything that would help me solve this… if it’s out there I completely missed it.

    I have a script that has numerous pages (ads) each has a counter that updates in a MySQL DB with the number of times the page has been viewed. Currently every time the page is reloaded then the count increments by one. Obviously that brings up the problem of the page just getting reloaded to
    increase the number of hits so I want it to check the session so that it would update only once even if the same person refreshed the page several times.

    So here is the part of the script that updates the DB

    PHP Code:
    $tell=$sitehits+1;
    $s "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid";
    $result1=q($s); 
    Below is what I managed to put together, it works. When the user goes from one page (ad) to another it increases the counter by one, if the user returns to a previously visited page it does not. But it’s not exactly the way I want it:

    PHP Code:
    if(!isset($_SESSION['page'])){ $_SESSION['page'][] = $_SERVER['REQUEST_URI']; }

    if(!
    in_array($_SERVER['REQUEST_URI'], $_SESSION['page'])){


    $tell=$sitehits+1;
    $s "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid";
    //$result1=q($s);

    $_SESSION['page'][] = $_SERVER['REQUEST_URI'];

    How do I set it up so that the SESSION will expire after a certain amount of time? Because currently if the user returns (which a lot of users do) to the site the following day, the counter does not update any pages he/she previously visited.

    Thank you very much for any help anyone can provide
    Last edited by mariodac; 12-02-2010 at 03:07 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Cookies are more in line with what you'd like-- a storage area that expires after an amount of time. That pretty much describes a cookie exactly.

    If you are adverse to cookies (which you shouldn't be, if you're using sessions, since PHP sessions use the cookie system by default), then you can create a session variable that stores a timestamp at the same time you set your session, and then add a check of that timestamp when you find the session. If it's old enough, update the counter and reset the timestamp.

  • #3
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    You should take a look at the post I made on need help with php script

    This gives an example of IP based counters.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    Cookies are more in line with what you'd like-- a storage area that expires after an amount of time. That pretty much describes a cookie exactly.

    If you are adverse to cookies (which you shouldn't be, if you're using sessions, since PHP sessions use the cookie system by default), then you can create a session variable that stores a timestamp at the same time you set your session, and then add a check of that timestamp when you find the session. If it's old enough, update the counter and reset the timestamp.
    Fumigator
    Would you be willing to help me out with the code for the cookies?
    I have tried some, but for some reason could not get it to work
    Thank you for your help

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I can help you out with your cookies, sure. They are pretty straight forward; just don't try to use a cookie you've set until the page is reloaded.

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    I can help you out with your cookies, sure. They are pretty straight forward; just don't try to use a cookie you've set until the page is reloaded.
    I really appreciate the help
    here is my code that I need to include
    PHP Code:
    $tell=$sitehits+1;
    $s "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid";
    $result1=q($s); 
    Thanks again

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Where's your cookie code?

  • #8
    New Coder
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is the thing, I'm not sure where to start
    Currently there's no cookie in the page.
    As I said, I'm pretty new at PHP. Trying to learn by trial and error

    Let me know if you can still help me out
    Thank you

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Alright well, start with google, "php cookie tutorial" should get you enough info to dig in. The idea is to use the superglobal $_COOKIE array.

    I also found the PHP manual to have a very good description of cookies, but some people find manuals difficult to use for some reason. Http://php.net

  • #10
    New Coder
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The part that confuses me is this
    The page that I need to insert the cookie is a Dynamic page.
    So when the user visits another AD page (same detail.php file)

    How is that going to work? It sees the same cookie so the counter will not update.

    Sorry if this is turning to be too much of a pain...

  • #11
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Sounds like you want a new cookie for each dynamically loaded page. If you name the cookies using some unique page identifier, that'll work fine.

  • #12
    New Coder
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok
    I did some searching around and found some similar code I think would work.
    I implemented it with a few minor changes to fit my code.
    It does work, except when the user goes to another Dynamic loaded page, it will not count until the time specified has expired.

    How do I make the following code apply to dynamically loaded pages

    For example: User visits page X the counter increases
    User visits page Y the counter increases (this is not happening with the current code displayed below)
    User visits page X before the time set expires and the counter DOES NOT increase -- The counter will only increase after the set time has expired


    PHP Code:
    $minvar 10//minutes before counter increments resume.

    $tell=$sitehits+1;
    $s "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid"//original code

    if(!$_SESSION['page']) {  
    $_SESSION['page'] = time();
    $result1=q($s) or die(mysql_error()); 

    }
    elseif((
    $_SESSION['page'] + ($minvar*60)) < time()) { 
    $result1=q($s) or die(mysql_error()); // starts recounting after the time set expires
    $_SESSION['page'] = time();

    I could not figure out how to set cookies for each dynamically loaded page

    Any suggestions to fix this code?

    Thank you

  • #13
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    It's really simple, you need to store a separate session variable, or cookie, or whatever, for every dynamically created page.

  • #14
    New Coder
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK
    I have no clue on how to get that done
    I have tried it, but no luck

    Can you give me an example?

    Thanks

  • #15
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I noticed you have a site ID. Use that for your $_SESSION index name instead of "page".

    PHP Code:
    $_SESSION[$siteid] = time(); 


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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