- Colection of 65 PHP scripts for $4.29 each
It is always good to know how many visits a web page has. Most of the hosting companies offer great tools for monitoring your web site visitors behavior but often you just need to see how many times a web page has been visited. Using PHP and a plain text file this has never been easier. The example below uses a flat-text database so no MySQL database is needed. I am currently working on a more advanced counter script which gives a lot more details about each visits and I will post it on site when it is done.
First, lets make a new php page and also a count.txt file with just 0 digit in it. Upload the count.txt file on your web server. Depending on server configuration you may need to change the permission for this file to 777 so it is writable by the counter script that we are going to use. Now, lets do the actual PHP code.
First line opens the count.txt file and reads the current counter value. Then the trim() function is used to remove any new line characters so we have an integer. On the next line we increment the current counter value by one and then using the fopen, fwrite and fclose commands we write the new value to our count.txt file.
Please, note that this counter is not suitable for web pages that have many visitors. A large number of file read/writes may corrupt the counter value. For high traffic web sites you should consider using a MySQL based web counter script.
First, lets make a new php page and also a count.txt file with just 0 digit in it. Upload the count.txt file on your web server. Depending on server configuration you may need to change the permission for this file to 777 so it is writable by the counter script that we are going to use. Now, lets do the actual PHP code.
<?php
$count = file_get_contents("count.txt");
$count = trim($count);
$count = $count + 1;
$fl = fopen("count.txt","w+");
fwrite($fl,$count);
fclose($fl);
?>
First line opens the count.txt file and reads the current counter value. Then the trim() function is used to remove any new line characters so we have an integer. On the next line we increment the current counter value by one and then using the fopen, fwrite and fclose commands we write the new value to our count.txt file.
Please, note that this counter is not suitable for web pages that have many visitors. A large number of file read/writes may corrupt the counter value. For high traffic web sites you should consider using a MySQL based web counter script.
14 Comments to "Make PHP counter"

Samuel Poe / March 22, 2022 at 14:11 pm
If you don't nothing about html counters of various kinds, don't get involved with this. The teacher expects you to know everything already! That's the problem! Where do you change the "permission" for server configuartion, will drive you up the wall. After uploading the count.txt file, right click on your count.txt file at your webhost or through ftp, then select file permissions which is at the bottom. Enter 777 or 666 if 777 does not work. If you have more than one directory (folder as in
www.evol.com/love you must place the count.txt file in that directory (folder) and all others if you have several directories or folders. Same applies to the
file! Place it in all directories or folders. I don't know about server configuration pertaining tofiles. Investigate by visiting some of your pages then write your domain address: www.?.com/count.txt. Change the ? to your domain name. If you have more than one folder, you have to write: www.?.com/?/count.txt. You may have to do that with all your pages. Like this: www.?.com/!.html or www.?.com/?/*.html. If your count.txt shows no number higher than 0, it don't work. Always place a 0 to start with. This page is nearly 11 years old. Changes have happened that are for the worse. So if it does not work, it's not the teachers fault.






