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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts

    hit counter tutorial?

    Hi, I've been planning to get into php for a while now but it's quite daunting after relatively simple languages like HTML and Javascript, so to finally make a start somewhere I got the idea to make a simple hit counter for my website.

    I guess it's one of the most simple things to do with php... Though I could be totally wrong here.

    I searched for some sort of a tutorial, but I only get premade hitcounters. If anybody could supply me with a link or somewhere to start I would be most gratefull.

    Thanks

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I can give you some hints to get started ....

    1) Your page(s) will have the extension .php (instead of .html).

    2) Where your counter appears, you have a PHP script that
    reads in the counter value from a file (an integer that gets incremented
    each time the page is viewed). That PHP script would either display that
    integer or convert the digits to .gif images (for a graphical counter). Once
    it displays the count, it increments the value and re-writes it back into
    that file.

    So, you'll have a file called: "count.dat" and this is the contents: 10236

    Your webpage ("index.php") would be something like this:
    Code:
    <html>
    <head><title>My Site</title></head>
    <body>
    This is my site.
    <br><br><br>
    
    <?php
    $fp = fopen ("count.dat", 'r');
    $count = fread ($fp, 1024);
    fclose ($fp);
    
    echo $count;
    
    $count=$count+1;
    $fp = fopen ("count.dat", 'w');
    fwrite($fp, "$count\n");
    fclose ($fp);
    ?>
    
    </body>
    </html>
    NOTE: Make sure you set the file permission ("count.dat") to 777 so you
    can write to the file ... to change the value.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    Thanks for the help. So, just to make sure I understand it all correctly, I should have:

    1 - an external "count.dat" textfile with nothing in it but 10236? (why 10236?)

    2 - an index.php file that is running the script?

    3 - a php script somewhere in index.php?


    Is it possible to make an external php script and then place a reference to it in all the pages I want it (counter in this case) to appear?

    If yes, would this prevent me from having to rename all my .html pages to .php?
    Last edited by Kirl; 11-06-2006 at 07:21 PM.

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    <?php
    include(counter.php);
    ?>
    your page here

    <?php echo "$count"?>//place this where you want your counter

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I just picked the number arbitrarily.

    Displaying counter on other pages? Do what Rafiki says ... but
    it would be the counter from the main page. If you want unique
    counters on each page, you would need to make each page have
    an extension .php and a unique "counter.dat" file for each one.
    "counter2.dat", "counter3.dat" ... etc.

    Yes, so PHP "include" is easiest, but I thought you might want to
    work with some PHP scripting to experiment with parsing the
    integer and using some .gif images for numbers ... something you
    can experiment with yourself.

    Even with PHP "include", you still need to increment the number, so
    you'll have to play with file read/write anyway.

    If your HTML file has the extension .php, it is parsed by the server
    as a PHP script. You'll see that the PHP script is put right inside
    your HTML file (index.php). That's the whole powerful thing about PHP.
    It can then be used to dynamically create your webpage. Not just a site
    counter, but you can have it manipulate content, images, databases, etc.

    Let your imagination go wild.
    Last edited by mlseim; 11-06-2006 at 08:39 PM.

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    Ah, that is what I thought. Thanks for elaborating.

    Eventually I want to make a very basic highscore list for my javascript snake game. So yes, some experimentation is in order...

    Thanks for the help so far, I'm going to try this out!

  • #7
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    Great, it's working. Suddenly doesn't seem as daunting as before, now it's just a matter of getting familliar with the commands.

    However, count.dat doesn't actually increment with each visit, only when refreshing the page. I tried fooling around with it a bit, but that didn't work out...

    Any idea what might be wrong?

  • #8
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    Ah, never mind, it was because the page loaded from cache instead of from the server...

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    You could increase your sitemeter accuracy by using cookies
    to only allow your current visitor one counter update no matter
    how many times they hit refresh. But that's more advanced.

    Learn basic PHP scripting by searching Google for many terrific
    PHP tutorials available online. Viewing and playing with other
    scripts is also a nice way to learn. Create a separate directory
    in your webspace where you can play around with scripts and
    not affect the rest of your site. Sort of like a "sandbox".

    Have fun with it. It's addicting.

  • #10
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    www.w3schools.com
    thats where i started with php, there are more advanced tuts out there when your done with that

  • #11
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    Yes, w3schools is an excellent resource!

    Thanks for the jump start guys, I'm diving in headfirst now. I'm sure I'll be back with some questions about a highscore table in the future.


  •  

    Posting Permissions

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