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
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Counter resetting for some reason

    I have several pages that all use the following counter code. The problem is that it seems to keep resetting back to zero (or some other very low number, I don't really know for sure). I have set it to several large numbers like 2 million and it works fine for a while then I go back and it shows something like 243 or whatever. It does not happen right away, sometimes it is daily, some weekly, others monthly.

    PHP Code:
    $fp=fopen("sheep_counter.txt","r");

    //Read the previous count
    $count=fgets($fp,1024);

    //close the file.
    fclose($fp);

    $fw=fopen("sheep_counter.txt","w");

    //Increment the counter
    $cnew=$count+1;

    //write the counter back to the log file ie., sheep_counter.txt
    $countnew=fputs($fw,$count+1);

    //Display VISITOR NUMBER
    echo "<br> <p align=center> You are the $cnew  Visitor to This site";
    fclose($fw); 
    =========
    Thanks for your time!
    Mitch

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,389
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    For a 32-bit system, the maximum integer size is: 2147483647
    So that's not your problem.

    The next thing I'm thinking about is multiple access at the same time.
    You might need to file-lock it so it can't be written by two people
    at the same time .... see this: http://www.hudzilla.org/php/8_11_0.php

    Finally, you could separate so each page uses it's own text file and
    use a separate PHP script to analyze all of them together. That would
    also allow you to check the counts on each of them separately.

  • #3
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry about the confussion, I have several pages that use the same code in their own individual pages, and DO write to seperate files.
    =========
    Thanks for your time!
    Mitch

  • #4
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,389
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    It might be interesting to experiment.
    Try flock (file locking) on one page, but not
    the others. See of that one page works.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,960
    Thanks
    2
    Thanked 304 Times in 296 Posts
    There is a brief period of time when one access will execute the fopen("sheep_counter.txt","w"), this will cause a zero length file to exist. If another visitor accesses at this time, he will read a zero length file (he might get a read error as the file would currently be open for writing.) In either case, the second visitor will unconditionally do his own fopen "w" and write a 1 into the file.

    There needs to be some error checking and if there was a read error, skip writing to the 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.

  • #6
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So if I do some sort of If..Then to see if the file is currently locked, then what? Can I deley and then try again or just cancile the writting all together. If so, then my code isn't much use as a counter I guess.

    What is the best way to keep track of how many time the page was hit?
    =========
    Thanks for your time!
    Mitch

  • #7
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Would setting up some sort of deley then retry to see if it is locked or not work?

    I mean, check to see if it is locked, if so, then deley and check again, repeat until it is open then do the write.

    If so, what would the code be?
    =========
    Thanks for your time!
    Mitch

  • #8
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,389
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    Isn't that the whole point of using flock?

    http://www.w3schools.com/PHP/func_fi...p?output=print

    Why not lock the file and do what you want.
    Nobody else can write to it while it's locked.

  • #9
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Isn't that the whole point of using flock?

    http://www.w3schools.com/PHP/func_fi...p?output=print

    Why not lock the file and do what you want.
    Nobody else can write to it while it's locked.
    Please correct me if I am wrong, but won't flcok cause the conflicting writes not to be written at all? Which is why I mentioned it not being a good counter then.
    =========
    Thanks for your time!
    Mitch


  •  

    Posting Permissions

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