What happens when many requests are received to read & write to a file in PHP? Do the requests get queued? Or is only one accepted and the rest are discarded?
I’m planning to use a text based hit counter.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can encounter the problem of race condition
To avoid this if you only need simple append data you can use
and don’t worry about your data integrity.
If you need more complex operation you can use flock (used for simple reader/writer problem)
For your PHP script counter I suggest you to do with something like this:
This way you don’t have to implement a blocking mechanism and you can keep the system and your code script lighter
Addendum
To avoid to have to count the
array file()you can keep the system even lighter with this:Basically to read the number of your counter you just need to read its filesize considering each impression add 1 byte to it