Unique Hit Counter
PHP Tutorial, Tutorials May 21st, 2008This is a PHP hit counter that logs one occurrence of each visitor’s IP address. It then displays an accurate count of the total number of unique visitors to your website.
The script is placed on your page wherever you would like the hit count to show (usually on a home page). Every time that page is accessed, the script checks a log file for the user’s IP address. If it finds a match, the count is not advanced. If it does not find a match, the count is incremented by one and the new IP is appended to the log file. The count is displayed on screen.
Files You will need crip_hits.php and crip_hits.txt (should be blank).
Brief explanation on crip_hits.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /* Don't forget to CHMOD $file to 777 */ $file = "crip_hits.txt"; // Obtain users IP address $ipadd = getenv(REMOTE_ADDR); $addip = "TRUE"; $hits = 0; if (file_exists($file)) { } else { echo "$file does not exist!"; exit; } |
The above code will obtain the ip of the visitor and also checks for the log file, it displays a error message when the log.txt file is missing and ends the script.
This part opens the log file and checks for the ip of the visitor and ends if the ip is already logged.
Lastly, if the ip of the visitor is NOT logged. The script will open the .txt file and log the ip. It than displays the count on the page.
Embedding PHP in HTML
If you want to embed the counter to your html pages, you will need to edit the .htacess file in your Apache Server.
1 | AddType application/x-httpd-php .htm .html |
INSTALLING
- Copy the script from crip_hits.php and paste it directly into your HTML code where you want the count to show. Save the file.
- Upload the saved file and crip_hits.txt to the same directory (folder).
- Upload the .htacess file to where your main index page is located
- CHMOD crip_hits.txt to 777.
- CHMOD .htacess to 777.
NOTE: You must upload the blank crip_hits.txt file for the script to work correctly. If crip_hits.txt is not present when the script is executed it will display an error message in the count! The file will not be automatically created!
To Download the script above : Click Here!
June 11th, 2008 at 11:35 am
Lol, what a script .. It uses damn old techniques. Never use quotes around variables nor around defines. For the IP-address use $_SERVER['REMOTE_ADDR'] instead. See it as feedback, because it is not good to teach wrong techiques.