last day (15 days later) » 

12:45 PM
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 counter updating script is: $handle = fopen("counter.txt", "r"); if(!$handle){ echo "could not open the file" ; } else { $counter = (int ) fread($handle...

 
What are you expecting the line fclose ($handle) ; $counter ; } to do? Specifically, the last part where you just have the variable name on a line on its own?
 
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.
 
What have you tried to debug the problem? Have you checked whether your devices request a proper, new page - or read them from their cache?
 
So, in your counter.txt file there's a 1, but on Desktop it shows 2? Btw, Heroku has Postgres-support.
 
12:45 PM
@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
 
What have you checked? Your server's access log?
 
@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
 
I didn't ask for the file containing your counter. There's no way to check for errors in that file if it only contains single digits. What does your server's log tell about the different accesses?
 
@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.
 
12:45 PM
Your code seems to be correct (not perfect as pointed out by @ProfessorAbronsius though) and works as desired on my local XAMPP webserver. Your link is also showing the right counter value after refreshing the page several times. You said you have this code embedded in more code. Can you share this here to make sure it doesn't influence the other code?
 
@Johannes Does it shows correct values for desktop devices too?
 
Yes @Andy, works as expected.
 
Can you check | counter link: link This is where someone will come and it will write in counter.txt link
 
Your second link is pointing to some strange video preview... @Andy. Please send the correct link to the PHP script please.
 
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
 
12:45 PM
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
 
@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!
 
12:45 PM
Andy, please be so kind and setup your project locally or protect it with a password (e.g. by using a htaccess file). Then try your test scenario again. The counter should work fine. I agree with @ProfessorAbronsius that the "double increment" happens due to parallel requests (e.g. by us) going on at the same time. The code you sent also seems fine, however I'm not able to spot this HTML code in the preview you shared.
 
@Johannes check this: ctxt.io/2/AACg0Zk_Ew However this has not to do anything with updating the counter. the counter is updated when some visits that video preview you earlier said about. And about updating the counter.txt file is mentioned above which is present inside video preview which updates the counter
 
Have you checked the local or protected setup I proposed and is your issues resolved now? If not: can you please rephrase your question?
 
@Johannes Yes I have tested it on local, protected before putting it up here. but it seems abnormal. it displays wrong value for desktop and laptops and correct for android and ios
 
Just tested again: refreshed the counter.txt file multiple times ( 112 hits each time ), loaded script to update counter once, checked counter =113. This seems A-OK!
 
@ProfessorAbronsius have you tested it on laptop or desktop? it works fine on android and ios.
 
12:45 PM
windows 7 laptop
 
@ProfessorAbronsius idk. I am getting this error. Have you changed anything? My test: default views = 120. after refreshing , views = 122 (it should be 121)
 
You wrote "refreshing views" (plural). Are you refreshing multiple windows/tabs on PC or only one?
 
I cannot change anything. My advice would be: change the name of the logfile ( ie: counter.txt ) to a temp name of your choosing, test again without multiple hits from other users. Also - create a ultra basic script to test this with - no javascript, no css - just the php code
 
@Johannes I think I have a comma there. which says I am refreshing it once. But plural is for views which is more than 1
@ProfessorAbronsius sure. I'll will check that. and update here
 
Yes Andy, but if your refresh two pages/views pointing to the same script on desktop it makes sense that the counter increases by two because every refresh of the page increment the counter by one. Make sure you only refresh one tab/page/view.
 
12:45 PM
Yes @Johannes I am refreshing it only once.
@Johannes It is working fine when testing without much scripts and css. The link to it: gdrive-x-premium.herokuapp.com/record.php
@ProfessorAbronsius check this: gdrive-x-premium.herokuapp.com/record.php working fine! I don't know what is conflicting in other one with css and scripts
 
that seems fine - but how many people are hitting that endpoint to test this...nobody knows!
 
@ProfessorAbronsius yeah! Now i will debug with original. what is actually causing that. thanks for your advice about testing it with basics. that worked!
 
I'll just post something that might help ~ 3 files designed to work together as a simple test - the file generated will be unique per person so you will be able to deduce by ip ( ip2long) if same requester or from different sources
 
@ProfessorAbronsius I am new on stack overflow. How should I close this question?
 
You can't close it as such - you can delete it but you need to decide whether there is anything others might learn from your experience. If it is simply a bug in your code then it is probably of little relevance to others - if it is agenuine issue with heroku hosting (?) then it perhaps has merit
 

last day (15 days later) »