last day (15 days later) » 

10:23 AM
0
Q: Getting abnormal error in Page View Counter using PHP

AndyI have page view counter script which fwrite in a .txt file and I echo it in another file where I have to display the page views. The view counter code is: $handle = fopen("counter.txt", "r"); if(!$handle){ echo "could not open the file" ; } else { $counter = (int ) fread($handle,20); ...

 
ultimately you are going to run into problems using a text file to record page views - multiple concurrent requests, file locks and file corruption are all possible. Is there a specific reason you do not use a db?
 
@droopsnoot sorry. I didn't recognize that. But the error is not that. Please check. Also i have edited the question. thanks for that
@ProfessorAbronsius I am a student and using Heroku for testing purpose. So no database. If you could help with above code, that would be really be helpful.
@NicoHaase Yeah. I have checked. It doesn't read from cache. When someone visits a page the above code writes it and another code reads and displays it. But on desktop devices it shows wrong count with additional +1 page view
@brombeer No. not exactly. For desktop devices it writes +1 page view. Idk why it does that? For android or IOS it writes correct page views in counter.txt
@NicoHaase Hello, here's counter.txt link: link And here's link which writes in counter.txt = link
@NicoHaase I don't understand. What logs are you talking about? There's nothing about different access. It just adds up 1 extra view for desktop devices.
 
There is nothing in the above code that would explain why a desktop device miscalculates the number of hits where mobile devices do not.
 
@Johannes Does it shows correct values for desktop devices too?
Can you check | counter link: link This is where someone will come and it will write in counter.txt link
Hello @ProfessorAbronsius, I have mentioned the php file which writes the page views. The link I shared is where the code is getting executed and gets written in counter.txt
 
Hard to know if the effect of the incorrect counter is a fault in the code or because multiple requests are being sent to test this problem. It appeared to me that it increased by two each time
 
10:23 AM
@Johannes here: ctxt.io/2/AACgivgiEw
@ProfessorAbronsius This is problem in counter itself. because it is in development and no request are being sent to it.
 
simply refreshing the counter.txt file shows sometimes a difference of 1 sometimes 2 - but that is probably because several people are hitting the page that updates the counter. The PHP code is flaky but appears correct - with multiple testing that same endpoint it is impossible to say definitively that there is or is not an issue. Without seeing the full code involved it is easy that something simple is being overlooked
 
@ProfessorAbronsius Thank you for your advice. if you get any update on this please let me know.
 
@brombeer pointed out that Heroku has Postgres db support.... I'd investigate that
 
@ProfessorAbronsius sure! thanks!
 

last day (15 days later) »