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:
<?php $counter = 0; $write = false; $filename = "link/to/file/hits.txt"; $ip = $_SERVER['REMOTE_ADDR']; $file = fopen($filename, 'r'); $content = file_get_contents($filename); $file_content = explode(" ", $content); fclose($file); for ($i = 1; $i < count($file_content); $i++) { if ($file_content[$i] != $ip) { $write = true; } else { $write = false; break; } } $file = fopen($filename, 'w'); if ($write) { fwrite($file, $content . " " . $ip . " -"); } else { fwrite($file, $content); } fclose($file); $file = fopen($filename, 'r'); for ($i = 1; $i < count($file_content); $i++) { if ($file_content[$i] == '-') { $counter++; } } fclose($file); echo $counter; ?>
Select what you want to copy and in doing so you will keep the formatting when pasting it. |
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:
Unique Page Views:
Select what you want to copy and in doing so you will keep the formatting when pasting it. |
And here's an example of what the txt document should look like after a few page hits:
Unique Page Views: 12.345.678.912 - 34.567.891.234 - 567.89.123.45 - 67.891.234.567 - 891.23.456.789 -
Select what you want to copy and in doing so you will keep the formatting when pasting it. |
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:
<html> <body> Page Hits: <?php include"link/to/file/page_hits.php"); ?> </body> </html>
Select what you want to copy and in doing so you will keep the formatting when pasting it. |
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