Results 1 to 4 of 4

Thread: Hit Counter

  1. #1
    Senior Member
    Join Date
    Oct 2001
    Posts
    677

    Hit Counter

    After reading Jethro's post on Website Administration, I felt compelled (ok, maybe not compelled, but inspired) to write something marginally useful in PHP. The simplest thing I could think of that would be useful was a hit counter, so here it is.

    This one uses a file-based count system, but I'll write a database version of it some time in the near future. It has not been tested due to the fact that I don't have a working apache setup on this computer at the moment, but I see no reason why it shouldn't work.

    PHP Code:
    <?

    /*
    PHP Simple Hit Counter
    Text File Version
    Andrew J. Bennieston
    */

    // Read Current Counter
    // If counter.txt does not exist, set counter to 0
    $file = fopen("counter.txt", "r");
    if ($file == false)
    {
     $counter = 0;
    }
    $counter = fread($file, 8);
    $ok = fclose($file);

    // Increment counter
    $counter++;

    // Write new count to counter.txt
    $file = fopen("counter.txt", "w");
    if ($file == false)
    {
     echo "Error: Could not create or open counter.txt";
    }
    $ok = fclose($file);

    // Print counter to screen:

    ?>

    <font size="5"><?= $counter ?></font>
    One Ring to rule them all, One Ring to find them.
    One Ring to bring them all and in the darkness bind them.
    (The Lord Of The Rings)
    http://www.bytekill.net

  2. #2
    er0k
    Guest
    spike i know this is old, and im bringing it back up. but i have a question.

    i didnt see a if true statement, maybe im just stupid but i figured something would have to be true. as you know im just learning php, and im guessing the file is just the page its set on? anyway thanks, its a cool tut.

  3. #3
    Senior Member
    Join Date
    Jul 2002
    Posts
    154
    You don't really use if true because by default statements are true, you only specify if something is going to be false because that is not done by default. So say I wanted to validate a user with an if statement by using a password, I wouldn't have to put in if true because as long as the password was right, it would be true, however, if I wanted to add in a message for the incorrect passwords, I would have to specify. Sorry, am I being coherent? Let me try again.
    When I use
    If(){
    }
    all that stuff only happens if what is required is true. So basically, everything already uses true, if true doesn't work, then you will see an error message.
    Does that make sense?

  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    677
    er0k,

    the:
    PHP Code:
    if ($file == false)
    {
     echo 
    "Error: Could not create or open counter.txt";

    if that is what you're referring to, works because fopen() returns a file identifier if the operation was successful, and returns false if it wasn't, so if $file == false then the file couldn't be created or opened... which means it can't be used. If $file !== false then it is a file identifier which can be used for read and write operations, depending on file mode specified in fopen().
    Hope that helps, i'll explain in more detail in IRC sometime if you require.


    Addendum:
    technically, it would've been better if i'd used:
    PHP Code:
    if ($file == false)
    {
    die(
    "Error: Could not create or open counter.txt");

    as this would've ended program execution instead of trying to display the counter anyway. It wuold also be fairly easy to match digits in the count to images, say 1.png or 2.png, to give a graphical count as opposed to a text-based count.
    One Ring to rule them all, One Ring to find them.
    One Ring to bring them all and in the darkness bind them.
    (The Lord Of The Rings)
    http://www.bytekill.net

Posting Permissions

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