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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    264
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Hit Counter Resetting

    I have a simple hit counter, but for some reason it resets itself every now and then. Here is the counter:

    Code:
    <?php
    $visits = ("hits.txt");
    $hits = file($visits);
    $hits[0] ++;
    $fp = fopen($count_my_page , "w");
    fputs($fp , "$hits[0]");
    fclose($fp);
    echo $hits[0];
    ?>

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    <?php
    $visits 
    "hits.txt";
    // open the file and read how many visitors was
    $fp fopen($visits,"r");
    $hits = (int )fgets($fp);
    fclose($fp);
    // increment the number of visitors
    $hits++;
    // open the file and write how many visitors are now
    $fp fopen($visits,"w");
    fputs($fp,$hits);
    fclose($fp);
    ?>
    could be done with a single fopen/fclose but how is now you can see more clear how to do each step.

    regards

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    264
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Didn't work dude. I think you forgot an echo, but not sure?

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by IFeelYourPain View Post
    Didn't work dude. I think you forgot an echo, but not sure?
    it work dude. and you don't need any echo. this do exactly what your previous code indend to do.

    showing the number of visitors in your page is another problem, isn't it? for that just echo $hits where you want.

    regards

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    264
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    it work dude. and you don't need any echo. this do exactly what your previous code indend to do.

    showing the number of visitors in your page is another problem, isn't it? for that just echo $hits where you want.

    regards
    I tried the exact script you posted up there and nothing happened.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by IFeelYourPain View Post
    I tried the exact script you posted up there and nothing happened.
    post a link to the page.

    regards

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Posts
    264
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    post a link to the page.

    regards
    Your script did not work at all. I tried it in a regular php page on my server. I found another script and will see how it works out.


  •  

    Posting Permissions

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