944,085 Members | Top Members by Rank

Ad:
  • PHP Code Snippet
  • Views: 10837
  • PHP RSS
0

A Reeeeaaly simple Hit counter

by on Oct 23rd, 2004
Ok, this is about as simple as it gets. This is a hit counter for your page that stores the count in a simple text file of your choosing. No need for a database, MySQL or otherwise. No complicated rubbish. Just one single function and one single file to store the count. Thats it.
PHP Code Snippet (Toggle Plain Text)
  1. <?php
  2. /* first you will need to create a text file somewhere and give it
  3. a name obviously. In this example I will call it hits.txt */
  4. $stored = "hits.txt";
  5.  
  6. /* The following function when called gets the text file
  7. named in the above variable '$stored', then opens it for
  8. reading and writing, saves its contents in variable $stuff
  9. closes it, then opens it again, adds 1 to $stuff,
  10. displays $stuff and then saves and closes the file again. */
  11.  
  12. function displayHitThingy($stored) {
  13. $fp = fopen($stored,rw);
  14. $stuff = fgets($fp,9999);
  15. fclose($fp);
  16. $fp = fopen($stored,w);
  17. $stuff += 1;
  18. print "$stuff";
  19. fputs($fp, $stuff);
  20. fclose($fp);
  21. }
  22.  
  23. /* Now you just display it on your page somewhere
  24. with the following */
  25. ?>
  26. Page hits: <?php displayHitThingy($stored); ?>
  27. <?php
  28. /* Thats it. You can of course use it on more than one
  29. page, just remember to change the textfiles name for each
  30. e.g. page1.txt page2.txt and so on.. */
  31. ?>
Comments on this Code Snippet
Nov 3rd, 2005
0

Re: A Reeeeaaly simple Hit counter

how is this simple

isnt there a program to downlad posibly


reese
Newbie Poster
Alona is offline Offline
11 posts
since Jun 2005
Nov 4th, 2005
0

Re: A Reeeeaaly simple Hit counter

Reese
This script requires PHP support on your server to work.
If you have PHP enabled, you copy and paste the script above into the page where you want it.
You will probably need to be in 'code view' or whatever the equivalent is in the software you are using.
You then have to rename the script to have a .php extension instead of a .htm or .html
Be aware that any other links to this page will need to be updated to reflect the name change.
Hope this helps you out
Dave
Master Poster
DaveSW is offline Offline
765 posts
since Jul 2004
Message:
Previous Thread in PHP Forum Timeline: Click Ads First..!!
Next Thread in PHP Forum Timeline: username=email address inputted on submission>is this possible





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC