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 behaviour 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 writeable 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. If you do not have one or need a custom build, do not hesitate to contact me.
| Comments |
| uchral posted on 2008-03-25 10:32:24 |
| hi thank you :) |
| Jose Beyer posted on 2008-02-16 17:16:52 |
| Do you have a PHP script counter with graphic number images? --------------- Veselin: we currently do not have but can build a custom one for you |
| Jefferson Mabanglo posted on 2007-11-08 02:00:37 |
| Thanks for the info. |
| rjdgreat posted on 2007-05-09 13:53:13 |
| hi, well it works but to get unique count, what do i need to do? thank you!! ---------------- PHPjabbers: you need more advanced counter which tracks only unique hits. Using cookie is a good option and we can make it for you for $30. We were unable to send you an email to your email address |
| Chris Gardner posted on 2006-11-03 11:27:04 |
| Hello, How do I set the permission for the count.txt file? Don't laugh, okay laugh, but I'm using Front Page 2003 (trying to learn Dreamweaver) and the site is on the YAHOO server. I created a txt file, uploaded it to the server, put the code above on the bottom of my page...so now do I just wait and check the .txt file on the server? thank you! ----------------------------- PHPjabbers: you can set file permissions using FTP program. |