Last updated on November 8th, 2024 at 12:34 pm
This code reads a number from a file, increments it, saves the new value back into the file, and displays it. Each time the script runs, the counter in hits.txt
increases by 1. However, this approach requires hits.txt
to have write permissions, and concurrent access might lead to race conditions where two requests try to read or write at the same time, causing inaccurate counts.
Here is the complete code.
<?php
$open = fopen("hits.txt", "r+");
$value = fgets($open);
$close = fclose($open);
$value++;
$open = fopen("hits.txt", "w+");
fwrite($open, $value); // variable is not restated, bug fixed.
$close = fclose($open);
echo $value;
?>
This PHP code is a simple counter script that reads a number from a file (hits.txt
), increments it by 1, writes the updated number back to the file, and then displays the updated count. Let’s break it down step by step:
- Opening the File in Read Mode
$open = fopen("hits.txt", "r+");
This line opens the filehits.txt
in read/write mode ("r+"
). If the file doesn’t exist, this code will produce an error. The$open
variable now holds a file handle to interact with the file. - Reading the Current Value“
$value = fgets($open);
This reads the current content ofhits.txt
, which should be an integer representing the current hit count, and stores it in$value
. - Closing the File
$close = fclose($open); - Incrementing the Counter
$value++;
Increments the value of$value
by 1, representing an additional “hit” or visit to the page. - Reopening the File in Write Mode
$open = fopen("hits.txt", "w+");
Openshits.txt
again, this time in write mode ("w+"
). This mode allows writing to the file, and it also truncates the file’s content to zero length, preparing it for new data. - Writing the Updated Value
fwrite($open, $value);
Writes the incremented$value
back intohits.txt
, updating the file with the new hit count. - Closing the File Again
$close = fclose($open);
Closes the file to complete the operation. - Displaying the Counter Value
echo $value;
Finally, it outputs the updated hit count to the browser, showing the current number of visits.
Note: This script works perfectly fine for low traffic website. As it is using simple text file as a counter there are lot of restrictions. High traffic website must use a Database like MySQL to increment the counter. CheckGraphical Hit Counter using PHP for more details on using database instead of text file.
thanks
easy to understand, thanks
No matter which way I enter code to read a text file or write a text file, for some reason my counter.php won’t read or write to that file. I have used 4 different ways to code it and I always end up printing 1 view. I tried debugging before I update the counter and $value never gets a value to it, just empty.
Dear Daniel,
First check if there is any extra charecters in your code when you tried to copy and paste the code above.
Second have you checked the permissions of the TXT file you are trying to R/W with the php script.
Third if none of the above worked pls paste the code here.I will try to rectify the issue.
Thanks.
hi, where do i put the php code, most of my website is in html but i’m using IIS php 5.2 Fast CGI. I dont want my hits displayed on my website, any advice, Just want it for personal reasons. Just started a blog.
please help
Hello Zain,
Since you are using html codes, i suppose all your files will have .html extension.So what you need to do is since your server support php you can do like this
1)Create a PHP file named hit.php
2)Copy and paste the entire code i have given in this post except echo $value
3)add an iframe in your html code
4)Call the hit.php from the iframe.If you want you can give the height and width of the IFRAME as 0%
4)This will get your counter hidden from others.
This will help you i believe.Do visit my website for more tricks thanks.
Its too simple.Just opening the txt file and incrementing var and closing the fileThats it.I didn’t thought that adding counter is so simple through PHP..Great! I will definitely try this.Thanks for sharing.
If you get error like : Warning: fopen(“hitstxtâ€) [function.fopen]: failed to open stream: No error in C:\wamp\www\test\counter.php on line 3
It’s because when you copied and pasted the code, the quotes are pasted as “hits.txt†instead of “hits.txt” in notepad.
Solution, just replace all “ with ” and, †with “.
0 + 0 = 0
so set the contents of hits.txt to 1
or use $value = $value + 1
Adding hit counter to your blog or site is always a good idea and using PHP script is great.Using PHP scripts over others has many benefits like PHP hits counter are open source and do not depend on browser configuration that’s why they track visitors perfectly.Thanks for sharing such an informative post.
this is not a sufficient code for counter , i am a new learner of php. i want to create a simple counter in which a box in which number is count.
Ok i try it
Great and useful information. Thanks for sharing this useful information.