SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Guru prequel's Avatar
    Join Date
    Nov 1999
    Location
    Brisbane, Australia
    Posts
    682
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why does basic counter reset itself?

    okay i put a basic counter on my vbulletin forums, i placed this in phpinclude template (which is the only template which can parse php code)
    PHP Code:
    $counterfile "counterfile.txt";
    if(!(
    $fp = @fopen($counterfile,"r"))) die ("cannot open counter file");
    $thecounter = (int) fread($fp20);
    fclose($fp);
    $thecounter++;
    $fp fopen($counterfile"w");
    fwrite($fp $thecounter);
    fclose($fp); 
    and referenced in my footer template

    $thecounter

    works for a while but resets itself ??? would changing
    PHP Code:
    fread($fp1024x1024); 
    help?

  2. #2
    SitePoint Member Ramses's Avatar
    Join Date
    Jul 2001
    Location
    Montréal, Canada
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: why does basic counter reset itself?

    I haven't tried it on your code, but perhaps locking the file [ flock($fp, LOCK_EX); ] would help avoid collisions:

    PHP Code:
    $counterfile "counterfile.txt";
    if(!(
    $fp = @fopen($counterfile,"r"))) die ("cannot open counter file");
    $thecounter = (int) fread($fp20);
    fclose($fp);
    $thecounter++;
    $fp fopen($counterfile"w");
    flock($fpLOCK_EX);
    fwrite($fp $thecounter);
    fclose($fp); 
    SciFlicks.com -- Science Fiction Cinema
    No, I'm not as old as Professor Farnsworth.

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, my instinct would be to use a database table to hold the counter (especially as you have a db connection already open in your script if u r using vBulletin)

    These SQL statements are only needed once:

    CREATE TABLE Counter (count INT(11) UNSIGNED NOT NULL);
    INSERT INTO Counter SET count = 0;

    Then in your php:
    PHP Code:
    // to get the value of count
    $result mysql_query("SELECT count FROM Counter");
    $count mysql_result($result0) + 1;
    // to increment the value of count
    $result mysql_query("UPDATE Counter SET count = count + 1"); 

  4. #4
    SitePoint Guru prequel's Avatar
    Join Date
    Nov 1999
    Location
    Brisbane, Australia
    Posts
    682
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freakysid
    Personally, my instinct would be to use a database table to hold the counter (especially as you have a db connection already open in your script if u r using vBulletin)

    These SQL statements are only needed once:

    CREATE TABLE Counter (count INT(11) UNSIGNED NOT NULL);
    INSERT INTO Counter SET count = 0;

    Then in your php:
    PHP Code:
    // to get the value of count
    $result mysql_query("SELECT count FROM Counter");
    $count mysql_result($result0) + 1;
    // to increment the value of count
    $result mysql_query("UPDATE Counter SET count = count + 1"); 
    thanks that was my next step in making it mysql loggable in my quest to learn php and mysql

    so i would create the Counter table within my vB database right? how would i reference where i want it should? using $count ?
    Last edited by prequel; Aug 26, 2001 at 17:24.

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes - you should add the table to your vB database.
    Yes - $count holds the value of the counter (I should have stuck with your variable names so not to confuse - same as your $thecounter)

  6. #6
    SitePoint Guru prequel's Avatar
    Join Date
    Nov 1999
    Location
    Brisbane, Australia
    Posts
    682
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that worked perfectly

  7. #7
    SitePoint Member
    Join Date
    Aug 2001
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use this little snippet at the beggining of all my indexes (could be set on all with a page column to sort.)

    PHP Code:
    <?
        $someuser 
    "user";
        
    $somepass "pass";
        
    $somehost "host";
        
    $logtname "log table here";
        
    $logdbname "site log db here";
        
    $link = @mysql_connect($somehost$someuser$somepass);
        
    mysql_select_db($logdbname);

        
    $querysql "SELECT * FROM $logtname";
        
    $result mysql_query($querysql);

        for (
    $i 0$i mysql_num_fields ($result); $i++) {
            
    $meta mysql_fetch_field($result);
            
    $colnames["$i"] = $meta->name;
        }
        
    $lasmoddate date("F j, Y, g:i a");
        
    $icolnames = @implode(","$colnames);
        
    mysql_query("INSERT INTO $logtname ($icolnames) VALUES('','".$REMOTE_ADDR."','".$HTTP_USER_AGENT."','".$lasmoddate."')");
        
    mysql_close($link);
    ?>
    I wrote this so that it would be fairly easy to a) maintain b) port to a lot of diffrent pages/ sites. I plan to make it a function but for now it is just a little include file. The function could easyily be done by something like:
    PHP Code:
    <?
    // I use 1 database for all of my various site loggs, then I use diffrent tables for each site or page.

    function loggem($somehost$someuser$somepass$logtname$logdbname) {
        
    $link = @mysql_connect($somehost$someuser$somepass);
        
    mysql_select_db($logdbname);

        
    $querysql "SELECT * FROM $logtname";
        
    $result mysql_query($querysql);

        for (
    $i 0$i mysql_num_fields ($result); $i++) {
            
    $meta mysql_fetch_field($result);
            
    $colnames["$i"] = $meta->name;
        }
        
    $lasdate date("F j, Y, g:i a");
        
    $icolnames = @implode(","$colnames);
        
    mysql_query("INSERT INTO $logtname ($icolnames) VALUES('','".$REMOTE_ADDR."','".$HTTP_USER_AGENT."','".$lasdate."')");
        
    mysql_close($link);
    }
    ?>
    It would be easy to include some other nice info like where they came from by ading a column to the table called "referrer" then updating my insert statement to:
    PHP Code:
        mysql_query("INSERT INTO $logtname ($icolnames) VALUES('','".$REMOTE_ADDR."','".$HTTP_USER_AGENT."','".$lasmoddate."','".$HTTP_REFERER."')");
    // It has to be misspeled to work! 
    the table contains 4 cols (ID, UserIP, UserExtInfo, VisitDate[, LinkedFrom]) but with this I can count the number of hits, finds duplicate hits or repeat visitors, track any time/date/day type variances etc.... you could also add that line i mentioned, referrer line, to find out where they are comming from or if they are typing your addy in there browser. any comments appreciated.
    Microsoft’s Motto: Resistance is futile, you will be assimilated…


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •