Coder Profile - Show off your skills, get a coder profile.
 
 
 
[PHP] Create A Unique Page Hits Counter
Programming
After coding through this for about 4 hours, I felt like I should share this

A hits counter is a counter that will display how many times your website (or a specified page) has been viewed. To start, open up a fresh text document and place the following code in:
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <?php
  2. $counter = 0;
  3. $write = false;
  4. $filename = "link/to/file/hits.txt";
  5. $ip = $_SERVER['REMOTE_ADDR'];
  6.  
  7. $file = fopen($filename, 'r');
  8.  
  9. $content = file_get_contents($filename);
  10. $file_content = explode(" ", $content);
  11.  
  12. fclose($file);
  13.  
  14. for ($i = 1; $i < count($file_content); $i++) {
  15.        if ($file_content[$i] != $ip) {
  16.               $write = true;
  17.        } else {
  18.               $write = false;
  19.               break;
  20.        }
  21. }
  22.  
  23. $file = fopen($filename, 'w');
  24.  
  25. if ($write) {
  26.        fwrite($file, $content . " " . $ip . " -");
  27. } else {
  28.        fwrite($file, $content);
  29. }
  30.  
  31. fclose($file);
  32.  
  33. $file = fopen($filename, 'r');
  34.  
  35. for ($i = 1; $i < count($file_content); $i++) {
  36.        if ($file_content[$i] == '-') {
  37.               $counter++;
  38.        }
  39. }
  40.  
  41. fclose($file);
  42.  
  43. echo $counter;
  44.  
  45. ?>
Now save it as a .PHP. I called mine "page_hits.php", but you can call it whatever you want. Be sure to edit the line $filename = "link/to/file/hits.txt"; to fit where your "hits.txt" page is; this will hold all of the IP addresses and number of hits your page has (again, I called mine "hits.txt", but you can call it whatever you want).

Now, this part is crucial:

You must have at least 3 sentences of text in your "hits.txt". I don't know why, but the script won't run without it.


Here's what I had:
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. Unique Page Views:
And here's an example of what the txt document should look like after a few page hits:
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. Unique Page Views:
  2.  12.345.678.912 - 34.567.891.234 - 567.89.123.45 - 67.891.234.567 - 891.23.456.789 -
FYI those aren't real IP addresses, just examples (In case you didn't know )

And that's pretty much it! Now, to link to your page hits counter, just insert this code into your page where you want the hits counter:
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <html>
  2.   <body>
  3.     Page Hits: <?php include"link/to/file/page_hits.php"); ?>
  4.   </body>
  5. </html>
You don't have to put the <html> and <body> tags in, those are just to show where the code should go.
Again, be sure to link to your "page_hits.php" script for this to work.


Explanation

For anyone who doesn't quite understand the script, here we go:

Basically the script will scroll through the txt document for the user's IP address. If it doesn't find it, it will add a hyphen - to the text document, along with the user's IP address. The number of hyphens represets the number of page hits (so 1 hyphen = 1 page hit). But if the script does find the user's IP address in the txt document, then it just ignores it and adds nothing to the txt document.
The script then displays (echo) the number of hyphens (page hits) for you to display.


Questions and comments are always welcome!

Best regards,
-Mike


Posted By mikeMarek
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
 
Anto     Posted 2.92 Years Ago
 
 
Great simple way to save the IP's ;)
 
Izzmo     Posted 3.70 Years Ago
 
 
Very nice. A good way to upgrade this is to expand it to the database.
Page 1 of 1
More Articles By This Author
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
if/else, switch/case and ?/: Statements In Actionscript 2.0
Recently Posted "Programming" Articles
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
My Experiences So Far with Languages
Selecting Your First Programming Language
[PHP] - Lets kill the complex IF
Basic PortScanner in VB6.0
N-Queens-Series Part I (Only the fittest survive)
Currying in JavaScript: Fun for the Whole Family!
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
Recently Rated "Programming" Articles
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
[PHP] - Lets kill the complex IF
Actionscript API
Actionscript Events
My Experiences So Far with Languages
Selecting Your First Programming Language
Vectors in C++
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Hardware (1)
Internet & Web Sites (3)
Life In General (2)
Networking (1)
Operating Systems (4)
Other (2)
Programming (46)
Security (8)
Software Development (5)
Web Development (14)
search Search Inside
Programming
 
 
Latest News About Coder Profile
Coder Profile Poll
Why do you get bored with programming?

Not enough time to do something productive
I run out of ideas
Too hard to show people my creations
Everything i do has too many errors, and it's too hard
I don't get bored!!!


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.20 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved