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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple Hit counter

    A hit counter will let us know how many times a page is accessed. In case one visitors loads the page several times, the hit counter will increase several times (but this is likely to happen only a few times).

    The code for the hit counter bellow will save the number of hits in a file named counter.txt (the name of this file may be changed). Each time the page is loaded, the file will be read, the number will be increased by one and the new number will be saved to the same file.

    <?php

    //The file where number of hits will be saved; name may be changed; p.e. "/counter_files/counter1.txt"
    $counterfile = "counter.txt";

    // Opening the file; number of hit is stored in variable $hits
    $fp = fopen($counterfile,"r");
    $hits = fgets($fp,100);
    fclose($fp);

    //increading number of hits
    $hits++;

    //saving number of hits
    $fp = fopen($counterfile,"w");
    fputs($fp, $hits);
    fclose($fp);

    //printing hits; you may remove next line (and keep the counter only for your records)
    print $hits;

    ?>
    To use this code, copy it to your page in the exact position where you want to show number of hits.
    Last edited by Inigoesdr; 10-13-2008 at 03:12 PM. Reason: Signature rules are there for a reason

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,470
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Who cares about hit counters anymore?

    My server logs and web stats provide me with all the info I need.
    The users don't care because I can make that number say anything I want it to say.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by mlseim View Post
    Who cares about hit counters anymore?

    My server logs and web stats provide me with all the info I need.
    The users don't care because I can make that number say anything I want it to say.
    Now, now; Don't be jealous you didn't think of it first.

  • #4
    New Coder
    Join Date
    Dec 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    clear this up

    Quote Originally Posted by markjon View Post
    $hits++;
    Does '$hits++;' and '$hits = $hits + 1;' do the same thing?

  • #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
    Quote Originally Posted by pauls74462 View Post
    Does '$hits++;' and '$hits = $hits + 1;' do the same thing?
    Yes..


  •  

    Posting Permissions

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