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 :: Page Hits PHP Script Copy / Restore
  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 :: Hits.txt Inside Code Copy / Restore
  1. Unique Page Views:
And here's an example of what the txt document should look like after a few page hits:
Code :: Page Hits Txt Document Copy / Restore
  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 :: Linking To The Counter Copy / Restore
  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.
 
Izzmo     Posted 136 Days 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
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
Quick Threading Tutorial - C++
C++ And Me (A Love Story)
First look at C
Introduction to Pseudo Code
Integrating your website with PHP
Recently Rated "Programming" Articles
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
N-Queens-Series Part I (Only the fittest survive)
[PHP] Create A Unique Page Hits Counter
Currying in JavaScript: Fun for the Whole Family!
Actionscript Events
First look at C
if/else, switch/case and ?/: Statements In Actionscript 2.0
Actionscript API
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Operating Systems (3)
Other (2)
Programming (48)
Security (10)
Software Development (5)
Web Development (15)
search Search Inside
Programming
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved