|
-
Feb 19th, 2006, 09:38 AM
#1
[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???
- ØØ -
-
Feb 19th, 2006, 09:43 AM
#2
Re: Simple counter gone wrong.
What happens if you place that statement in index.php?
-
Feb 19th, 2006, 09:53 AM
#3
Re: Simple counter gone wrong.
Same thing...just like it is called 3 times...
- ØØ -
-
Feb 19th, 2006, 09:56 AM
#4
Re: Simple counter gone wrong.
Just made a separate file for it...and now it works...weird...
- ØØ -
-
Feb 19th, 2006, 10:03 AM
#5
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...
- ØØ -
-
Feb 19th, 2006, 10:03 AM
#6
Re: Simple counter gone wrong.
A separate file for the function definition?
-
Feb 19th, 2006, 10:11 AM
#7
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.
- ØØ -
-
Feb 19th, 2006, 10:13 AM
#8
Re: Simple counter gone wrong.
Where's Adam Padam when you need him?
-
Feb 19th, 2006, 10:15 AM
#9
Re: Simple counter gone wrong.
On MSN tearing his hair off while talking to me...
- ØØ -
-
Feb 19th, 2006, 10:23 AM
#10
<?="Moderator"?>
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?
-
Feb 19th, 2006, 10:29 AM
#11
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:
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.
- ØØ -
-
Feb 19th, 2006, 10:33 AM
#12
-
Feb 19th, 2006, 11:33 AM
#13
-
Feb 19th, 2006, 11:39 AM
#14
Re: [RESOLVED] Simple counter gone wrong.
Why put a favicon in if it is missing?
-
Feb 19th, 2006, 11:44 AM
#15
-
Feb 19th, 2006, 07:54 PM
#16
Re: [RESOLVED] Simple counter gone wrong.
By the way, require_once is better for files you only want once per request.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 20th, 2006, 04:38 AM
#17
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.
- ØØ -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|