[RESOLVED] Simple counter gone wrong.
I have made a really simple counter. It stores the number of visits in a text file. But for some reason it adds 3 ever time you do a refresh in stead of 1. Can anyone explain why? This is the function I am calling:
PHP Code:
function total_page_views(){
$tot_user_file = $path . '/totalusers.txt';
$totuser_handle = fopen($tot_user_file, 'r+') or die("can't open file");
flock ($totuser_handle, LOCK_EX);
if (filesize($tot_user_file) > 0){
$totusers = fread($totuser_handle, filesize($tot_user_file));
$totusers ++;
}else{
$totusers = 1;
}
$totuser_handle = fopen($tot_user_file, 'w+') or die("can't open file");
fwrite($totuser_handle, $totusers);
flock ($totuser_handle, LOCK_UN);
fclose($totuser_handle);
return $totusers;
}
And I have split up the index file in 3 parts. So it is the index.php file, the top.php and the bottom.php. The index.php includes the top.php in the top, and the bottom.php on the bottom. So in the top.php file I have something like this:
PHP Code:
<?php
require "functions/counter.php";
echo ' <div id="banner">' . total_page_views() . '</div>';
?>
A bit awkvard I know...but hmm...anyone see what I might be doing wrong???
- ØØ -
Re: Simple counter gone wrong.
What happens if you place that statement in index.php?
Re: Simple counter gone wrong.
Same thing...just like it is called 3 times... :sick:
- ØØ -
Re: Simple counter gone wrong.
Just made a separate file for it...and now it works...weird...
- ØØ -
Re: Simple counter gone wrong.
To be honest, I have had the same problem before too. Then it was on a MySQL page where everytime I added a new record, it suddenly added 3 of the same records...I can't belive how bad I am at this...:D
- ØØ -
Re: Simple counter gone wrong.
A separate file for the function definition? :confused:
Re: Simple counter gone wrong.
Well, I have (or at least soon) one file with all functions for founters and stuff like that, so I can just use "require" from all the other files, and then call it.
- ØØ -
Re: Simple counter gone wrong.
Where's Adam Padam when you need him?
Re: Simple counter gone wrong.
On MSN tearing his hair off while talking to me...:)
- ØØ -
Re: Simple counter gone wrong.
Are you calling it in each of the file you include? (top.php bottom.php) You code that works the counter is fine, whats the rest of your code like?
Re: Simple counter gone wrong.
I just did as Visual just told me. I extended the function to write to a log file the UA string, to check if it is the browser that calls it 3 times. And it looks like it:
output for just visiting the page once:
Quote:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
John T: I am just calling it in top.php, which is included in the top of index.php. So in theory it should just be called once.
- ØØ -
Re: Simple counter gone wrong.
Hahahhaa...and the funny thing. It is only Fx it is not working..:D.. Opera and IE 7 only make it count 1 at a time.:D:D:D:D:D
- ØØ -
Re: [RESOLVED] Simple counter gone wrong.
Haha..this is the funniest bug ever...a missing favicon on the server made it Fx do 3 requests trying to find it, while other browsers only forget about it..:D:D:D took away the line for the favicon, and now the counter works..:D
- ØØ -
Re: [RESOLVED] Simple counter gone wrong.
Why put a favicon in if it is missing? :D
Re: [RESOLVED] Simple counter gone wrong.
It is just standard copy paste stuff on the top there...:D Same doctype, same name and stuff..:D..just new favicon and title..:D...grrr..
- ØØ -
Re: [RESOLVED] Simple counter gone wrong.
By the way, require_once is better for files you only want once per request.
Re: [RESOLVED] Simple counter gone wrong.
Yeah, I see your point, but not sure if it would have resolved it since even index.php was loaded three times.
- ØØ -