RSS

Hit Counter Tutorial - PHP

Mon, Sep 22, 2008

PHP, Tutorials

Graph

You may want a hit counter on your site, this can be done through some very simply lines of code.

The first thing you need to do is create a file called hits.txt

Once this has been created just close it, we won’t need to do anything with it. The next thing we need to do is create a PHP file. Call it what ever you want, it doesn’t matter in this case.

Then we can start coding…

The first bit of code to add to your PHP file is:


$hits = file_get_contents('hits.txt');

What this does it gets the number of hits from the hits.txt file. Then add…


$hits += 1

What this does is adds 1 to the number of hits.


file_put_contents('hits.txt', $hits);

Then this bit of code puts that into hits.txt

This next bit is optional, you can echo the number of hits you have on that page. You do this by the following bit of code…


echo $hits;

Then you will need to set the permissions of hits.txt to 777

Then thats it. Short and sweet ;)

Source: Download
Demo: View

Rating: 7.2/10 (6 votes cast)
Share and Enjoy:
  • Digg
  • Google
  • Technorati
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Furl
  • Live
  • Reddit
  • Spurl

  • Subscribe
    • XML
    • Google Reader or Homepage
    • Add to My Yahoo!
    • Subscribe with Bloglines
    • Subscribe in NewsGator Online
    • BittyBrowser
    • Add to My AOL
    • Convert RSS to PDF
    • Subscribe in Rojo
    • Subscribe in FeedLounge
    • Subscribe with Pluck RSS reader
    • Solosub
    • MultiRSS
    • R|Mail
    • Rss fwd
    • Blogarithm
    • Eskobo
    • gritwire
    • BotABlog
    • Simpify!
    • Add to Technorati Favorites!
    • Add to netvibes
    • Add this site to your Protopage
    • Subscribe in NewsAlloy
    • Subscribe in myEarthlink
    • Add to your phone
    • Get RSS Buttons

, , ,



This post was written by:

admin - who has written 41 posts on Unreal Media.


Contact the author

4 Comments For This Post

  1. Jay Says:

    Very simple, a good tutorial for beginners. Though, I do think that it should be expanded on. Then again, it’s a hit counter and not a statistics tracker.

    Rating: 0.0/5 (0 votes cast)
  2. admin Says:

    Yes, I was going to expand. Mabey make it into a stat counter. Something like Statcounter. Where the user would add an image or something then they could track stats and things. But it gets quite hard to track alot of things past pageloads and unique views.

    Rating: 0.0/5 (0 votes cast)
  3. Jim Spence Says:

    Thursday In searching for sites related to AdSense but more specifically to %KEYWORD, I found your site which has great content.

    Rating: 0.0/5 (0 votes cast)
  4. cornelia Says:

    Could you please translate this PHP tutorial so I can use it on my flash-made site? I used action script 2.0, I have not yet learned 3.0, I admit with embarrassment.

    Thank you,
    Cornelia

    Rating: 0.0/5 (0 votes cast)

Leave a Reply