Results 1 to 17 of 17

Thread: [RESOLVED] Simple counter gone wrong.

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Resolved [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_handleLOCK_EX); 
        
        if (
    filesize($tot_user_file) > 0){
        
    $totusers fread($totuser_handlefilesize($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_handleLOCK_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???



    - ØØ -

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Simple counter gone wrong.

    What happens if you place that statement in index.php?

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Simple counter gone wrong.

    Same thing...just like it is called 3 times...




    - ØØ -

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Simple counter gone wrong.

    Just made a separate file for it...and now it works...weird...


    - ØØ -

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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...


    - ØØ -

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Simple counter gone wrong.

    A separate file for the function definition?

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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.



    - ØØ -

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Simple counter gone wrong.

    Where's Adam Padam when you need him?

  9. #9

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Simple counter gone wrong.

    On MSN tearing his hair off while talking to me...


    - ØØ -

  10. #10
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    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?

  11. #11

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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.



    - ØØ -

  12. #12

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Simple counter gone wrong.

    Hahahhaa...and the funny thing. It is only Fx it is not working.... Opera and IE 7 only make it count 1 at a time.



    - ØØ -

  13. #13

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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.. took away the line for the favicon, and now the counter works..



    - ØØ -

  14. #14
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [RESOLVED] Simple counter gone wrong.

    Why put a favicon in if it is missing?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  15. #15

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: [RESOLVED] Simple counter gone wrong.

    It is just standard copy paste stuff on the top there... Same doctype, same name and stuff....just new favicon and title.....grrr..


    - ØØ -

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  17. #17

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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
  •  



Click Here to Expand Forum to Full Width