Welcome, Guest
  • Author Topic: Live Counter modification...how to?  (Read 3801 times)

    Air Data

    • Server what's that
    • *
    • Posts: 11
      • View Profile
      • Email
    Live Counter modification...how to?
    « on: 08/04/03, 14:18 »
    I have the following php code that works fine for my Live Counter but it is being reset. So, I'm trying the modification but cannot get it to work. The "modification" example doesn't seem complete to me...but then again I'm no php expert :) Any help appreciated!

    Existing Code (counter gets reset):

    <?

    $filename = "eLetter.txt";

    $fp = fopen( $filename,"r");
    $Old = fread($fp, 100);
    fclose( $fp );

    $Old = split ("=", $Old, 5);

    $NewCount = $Old[1] + '1';

    $New = "Count100=$NewCount";

    $fp = fopen( $filename,"w+");
    if (flock($fp, 2)) {
    fwrite($fp, $New, 100); }
    fclose( $fp );

    print "Count100=$NewCount";

    ?>

    Modified Code (this doesn't seem complete to me...no print statement):

    <?
    $filename = "eLetter.txt";

    $fd = fopen( $filename,"r");
    flock($fd,1);
    $Old = fread($fd, 200);
    fclose($fd);

    $Old = split ("=", $Old, 3);
    $NewCount = $Old[1] + '1';

    $Old[0] = ereg_replace("[^0-9]", "", $Old[0]);
    $New = "Count100=$NewCount";

    $fd = fopen( $filename,"a");
    flock($fd,2);

    $fp = fopen( $filename,"w+");
    fwrite($fp, $New, 200);
    fclose($fp);

    fclose($fd);
    ?>

    Thanks for any help.

    Ronald Wernecke

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 6203
      • View Profile
      • Professional Support
      • Email
    Re:Live Counter modification...how to?
    « Reply #1 on: 08/06/03, 05:29 »
    How about just this:


    <?php
    $hdl=fopen("ct.txt","rw+");
    $ct= fread($hdl,filesize ("ct.txt"));
    $ct=$ct+1;
    rewind($hdl);
    fwrite($hdl,$ct);
    fclose($hdl);
    echo "uct=".$ct;
    ?>

    Works just fine.

    Yo may rename the filename or the variablenames. But you dont need more.
    happy flashing
    8)
    Ronald

    Air Data

    • Server what's that
    • *
    • Posts: 11
      • View Profile
      • Email
    Re:Live Counter modification...how to?
    « Reply #2 on: 08/06/03, 07:17 »
    Thanks for the reply but I was hoping that someone could fix the modified version above. This counter is from the Live Counter example of Flash-db. The original php counter is being reset. So, in the Live Counter example there is a modification of the code, listed above, that is suppose to keep the counter from being reset but it does not work for me. I don't understand your code but I somewhat understand the Live Counter code and have used it extensively on a web site and would like to use it.

    Thanks for the help.

    Ronald Wernecke

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 6203
      • View Profile
      • Professional Support
      • Email
    Re:Live Counter modification...how to?
    « Reply #3 on: 08/06/03, 07:29 »
    I just had a look at your code.

    You are locking a file - and then you try to open it again for wrtiting.
    This actualy connot work.
    I don't understand what you are trying to do.
    happy flashing
    8)
    Ronald

    Air Data

    • Server what's that
    • *
    • Posts: 11
      • View Profile
      • Email
    Re:Live Counter modification...how to?
    « Reply #4 on: 08/06/03, 08:52 »
    I would like the make the file on Flash-db named "Simple Live Counter with PHP" work without being reset. Basically it works with a flash file that shows the count, a php file that opens a text file and increases the number in that text file as a variable for flash, then the flash file reads the text file. The problem is that I'm having too many people viewing the web page with the counter at the same time and the text file gets reset. So, in the docs of "Simple Live Counter with PHP" it has a "modification" that is supposed to keep the file from being reset and I can't get it to work. The basic php file works fine...except that it gets reset. Here is what it says in the docs about the basic method (Part 1) and then the modification (Part 2):

    PART 1
    For this part you can use the attached script by just adding it to the same directory as the PHPCounter.swf.  Not much else has to be done.  I'm going to go over the script in Detail however in the hopes that you'll have a better understanding of how it works and so that you can create your own modifications in the Future.  (For example purposes Line numbers where used here for the example - do not use line numbers in the actual script. 

    1)  <?
    2)  $filename = "PHPCounter.txt";
    3)  $fp = fopen( $filename,"r");
    4)  $Old = fread($fp, 100);
    5)  fclose( $fp );
    6)  $Old = split ("=", $Old, 5);
    7)  $NewCount = $Old[1] + '1';
    8)  $New = "Count=$NewCount";
    9)  $fp = fopen( $filename,"w+");
    10)  if (flock($fp, 2)) {
    11)  fwrite($fp, $New, 100);}
    12)  fclose( $fp );
    13)  print "Count=$NewCount";
    14)  ?>

    Line 1 - Tells the server to start processing this script as a PHP script. 
    Line 2 - Sets the variable filename to the name of the text file that we are going to be using to store the number of visits or Counts.  Remember in PHP whenever you see a $ that indicates a scaler variable.
    Line 3 - Opens the text file for reading.  
    Line 4 - This line basically places the content of the text file into the variable Old.  
    Line 5 - Closes the text file.
    Line 6 - This line splits the text file where ever an = sign occurs and puts the two parts into an array that can be referenced as follows:  $Old[0] references the Count part of the string, $Old[1] references the Number part of the string.  This is important because we only want to read and add to the number and not the whole string (Count=Number). 
    Line 7 - Adds 1 to the current Count. 
    Line 8 - Creates a new string that can be placed into the text file. We need to add the 'Count=' part again because we got rid of it when we split the string into the array.
    Line 9 - Opens the file for writing to.
    Line 10 - An optional argument that locks the file (file locking) so that we do not write over the same file at the same time - this can cause the Count to reset and may cause other errors.  This is mostly important if you think a large number of people will be visiting your site at any one moment. 
    Line 11 - This writes over the current text file with the New value - the count is increased by one.
    Line 12 - Closes the text file for writing to.
    Line 13 - Prints out Count=(The new Count) which is returned to the Flash movie - the value of Count then appears in the dynamic text box named Count in the Flash movie. 
    Line 14 - Ends the PHP script.

    PART 2
    Code Modification:  The following code works in exactly the same way as the above mentioned however there is increased file locking capabilities.  This is important if you think that your going to have lots of calls to this script at the exact same time.  File Locking (flock) makes sure that only one file is being written to at any one instant.  Notice that the main change to this script is the inclusion of the lines: $fd = fopen( $filename,"a");  flock($fd,2);  This creates a dummy file lock on that text file being used, which prevents the extremely rare case of the the script opening another write connection before it has time to lock it.  This is however extremely rare and in most cases does not have to be considered. 

    <?
    $filename = "FooterCounter.txt";

    $fd = fopen( $filename,"r");
    flock($fd,1);
    $Old = fread($fd, 200);
    fclose($fd);

    $Old = split ("=", $Old, 3);
    $NewCount = $Old[1] + '1';

    $Old <bracket>0<bracket> = ereg_replace("[^0-9]", "", $Old<bracket>0<bracket>);
    $New = "Count=$NewCount";

    $fd = fopen( $filename,"a");
    flock($fd,2);

    $fp = fopen( $filename,"w+");
    fwrite($fp, $New, 200);
    fclose($fp);

    fclose($fd);
    ?>

    Thanks again for your help.
    Note: <bracket> in the code above equals [ ], it would change when pasted in.
    « Last Edit: 08/06/03, 08:59 by Air Data »

    Ronald Wernecke

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 6203
      • View Profile
      • Professional Support
      • Email
    Re:Live Counter modification...how to?
    « Reply #5 on: 08/06/03, 09:12 »
    to make sure nobody is reading an old value while you are writing a new one, it is important, that you lock for rewriting right at the beginning.
    Why letting others read the same value?
    You open the file
    lock it, if you expect somebody else in this millisecond
    read the data
    add 1 to it
    write it back to the same place (thats the rewind in my code)
    unlock it (if it was locked)
    and close it again.

    If you keep the script as short as possible, the chance for other scriptes to com to the same time is low.

    happy flashing
    8)
    Ronald

    Air Data

    • Server what's that
    • *
    • Posts: 11
      • View Profile
      • Email
    Re:Live Counter modification...how to?
    « Reply #6 on: 08/06/03, 09:22 »
    I'm new to PHP and don't have a full understanding as I'm sure you can tell. How would you write that using Part 1 of the original code?

    Thanks again.

    Ronald Wernecke

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 6203
      • View Profile
      • Professional Support
      • Email
    Re:Live Counter modification...how to?
    « Reply #7 on: 08/06/03, 09:35 »
    I would write it with a little extension of my code:

    <?php
    $hdl=fopen("ct.txt","rw+");
    flock($hdl,2);
    $ct= fread($hdl,filesize ("ct.txt"));
    $ct=$ct+1;
    rewind($hdl);
    fwrite($hdl,$ct);
    flock($hdl,4);
    fclose($hdl);
    echo "uct=".$ct;
    ?>

    happy flashing
    8)
    Ronald

    Air Data

    • Server what's that
    • *
    • Posts: 11
      • View Profile
      • Email
    Re:Live Counter modification...how to?
    « Reply #8 on: 08/06/03, 14:09 »
    Just wanted to say thanks Ronald! Got it working and everything seems fine.


    Ronald Wernecke

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 6203
      • View Profile
      • Professional Support
      • Email
    Re:Live Counter modification...how to?
    « Reply #9 on: 08/06/03, 14:14 »
    Did you have a chance to test simultanioues access?
    I had no chance yet to check if it realy works under heavy access.
    But in theory it should.
    happy flashing
    8)
    Ronald

    Musicman

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 2685
      • View Profile
      • Email
    Re:Live Counter modification...how to?
    « Reply #10 on: 08/06/03, 17:09 »
    Hi Rinald,

    the code should work, nd on unix servers it does.
    Both the php and especially the perl doc warns that some other OS might not have a reliable implementation of flock()
    The proper file mode should be r+ (read, then write)
    The code quoted before is definitely wrong - the lock must be kept while reading and rewriting  the file

    Musicman
    « Last Edit: 08/06/03, 17:12 by Musicman »

    Air Data

    • Server what's that
    • *
    • Posts: 11
      • View Profile
      • Email
    Re:Live Counter modification...how to?
    « Reply #11 on: 08/06/03, 18:36 »
    Musicman, how would your rewrite the following code to keep it from being overwritten?

    <?

    $filename = "eLetter.txt";

    $fp = fopen( $filename,"r");
    $Old = fread($fp, 100);
    fclose( $fp );

    $Old = split ("=", $Old, 5);

    $NewCount = $Old[1] + '1';

    $New = "Count100=$NewCount";

    $fp = fopen( $filename,"w+");
    if (flock($fp, 2)) {
    fwrite($fp, $New, 100); }
    fclose( $fp );

    print "Count100=$NewCount";

    ?>
    « Last Edit: 08/06/03, 18:36 by Air Data »

    Air Data

    • Server what's that
    • *
    • Posts: 11
      • View Profile
      • Email
    Re:Live Counter modification...how to?
    « Reply #12 on: 08/07/03, 08:21 »

    Did you have a chance to test simultanioues access?
    I had no chance yet to check if it realy works under heavy access.
    But in theory it should.


    I have it up and running now...no problems yet...time will tell :)

    Air Data

    • Server what's that
    • *
    • Posts: 11
      • View Profile
      • Email
    Re:Live Counter modification...how to?
    « Reply #13 on: 08/07/03, 09:09 »
    I have another problem...I've just been checking the script by loading the text file into the browser. But, I need it to load into Flash. Flash needs a variable to load. How would I add a variable to the text file with Ronald's script? Something like the original code at the top. Notice how it was "split" so that it would read both sides of the "=" and then add the count to the number in the text file. The text file needs to look something like this: Count=11. Flash would use the variable "Count" to bring in the text.

    Thanks,

    Air Data

    • Server what's that
    • *
    • Posts: 11
      • View Profile
      • Email
    Re:Live Counter modification...how to?
    « Reply #14 on: 08/07/03, 10:35 »
    Ronald/Musicman,

    Do you think this would do the job? This seems to be working but I wanted to see if you thought this would lock the file so that counter wouldn't be overwirtten by too many people accessing the page at the same time. This gives me the variable 03Aug=1 in the text file which is what I need for Flash.

    <?
    $fp = fopen("03Aug.txt","rw+");
    flock($fp);
    $Old = fread($fp, 100);
    $Old = split ("=", $Old, 5);
    $NewCount = $Old[1] + '1';
    $New = "Count11=$NewCount";
    rewind($fp);
    fwrite($fp, $New, 100);
    flock($fp, 4);
    fclose($fp);
    print "03Aug=$NewCount";
    ?>
    « Last Edit: 08/07/03, 14:08 by Air Data »