A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: problems with flash + php counter

  1. #1
    Junior Member
    Join Date
    Sep 2004
    Posts
    13

    problems with flash + php counter

    Hi,

    I'm having problems getting a basic php counter to work on my site. I'm looking to have a small subtle flash movie at the bottom of my homepage (index.html) that shows the amount of visitors.

    I've created a flash movie (PHPCounter.swf) with one frame. There is a dynamic text box with the variable name "Count". In the frame there is the actionscript:
    Code:
    loadVariablesNum ("PHPCounter.php", 0);
    then I have a .php file (PHPCounter.php) with the following code:
    Code:
    <?
    $filename = "PHPCounter.txt";
    $fp = fopen( $filename,"r"); 
    $Old = fread($fp, 100); 
    fclose( $fp ); 
    $Old = split ("=", $Old, 5);
    $NewCount = $Old[1] + '1';
    $New = "Count=$NewCount";
    $fp = fopen( $filename,"w+");
    if (flock($fp, 2)) { 
    fwrite($fp, $New, 100); }
    fclose( $fp ); 
    print "Count=$NewCount";
    ?>
    and finally a file PHPCounter.txt with:
    Code:
    Count=1
    I've published the flash movie, added it to the homepage, uploaded all the files into the same root folder, and chmod both the php and text files to 666
    Unfortunately the counter does not seem to be working, and no matter how many times I refresh the page it never goes past 2.

    If anyone can give me some pointers on where I'm going wrong I'd be much obliged. The address is www.bonnieandclyde.biz.

    Thanks,

    Fanto.
    tick.. tock.. tick.. tock..
    boom boom boom

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    I tried this script both on your server and on mine, in a way that would not hit the browser cache, and while the local copy works perfect, yours resets the counter quite often.
    There is a potential problem with your script, but it should not cause problems unless you get to viewing rates ofmore than one visitor per minute (the script should open the file in r+ mode, lock it, and keep the lock until the end)
    There is another problem that viewing again may take the value from browser cache rather than asking for a counter hit. This again can hardly explain the observed behaviour

    Musicman

  3. #3
    Junior Member
    Join Date
    Sep 2004
    Posts
    13
    Hi Musicman,

    Thanks for looking at the problem.

    Unfortunately I'm a bit simple when it comes to php etc. Do you know if there is a way to solve this problem? Or do you have an alternative script that I might be able to use? As you can see I took the script from the tutorials on this site and just changed it a bit to suit myself because it wouldn't work for me.

    Again, thanks for your time and any more help will be greatly appreciated.

    Fanto.
    tick.. tock.. tick.. tock..
    boom boom boom

  4. #4
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    here is the counter code I use:
    PHP Code:
    <?

    $filename = "counter.txt";

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

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

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

    $New = "varCount=$NewCount";

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

    echo ("varCount=$NewCount");

    ?>
    then in flash, i use this code to retreive the data and make the hit...
    Code:
    function displayCounter(counter_url:String, counter_textbox:String) {
    	var varphp:LoadVars = new LoadVars();
    	var varcounter:LoadVars = new LoadVars();
    	varphp.ran = random(999999);
    	varphp.sendAndLoad(counter_url, varcounter, "GET");
    	varcounter.onLoad = function(success) {
    		if (success) {
    			eval(counter_textbox)=varcounter.varCount;
    		}
    	};
    }
    and an example of using this would be:
    Code:
    displayCounter("counter.php", "Count");
    and that would load 'counter.php' and display its contents in the textfield 'Count'
    Last edited by MyFriendIsATaco; 08-15-2005 at 08:22 AM.

  5. #5
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    you are already taking care of the cache with that random piece.
    Nowm the proper sequence of events for updating a file would be

    $fp = fopen($filename, "r+"); // open for read, later write
    flock($fp, LOCK_EX); // wait until we have the file for ourseleves
    $old = fread($fp, 100); // read data
    Now, at this point dont close the file !!
    Rather, later:
    fseek($fp, 0, 0); // set file position to start
    fputs($fp, $new); // save new data
    fclose($fp); // finished - this will release the lock

    As I said before, this will help against a common problem of concurrent access (and despite statistics this kind of problem even happened with a guestbook receiving less than 10 entries a day)
    I dont know how frequented your site really is, but in order for this collision to happen a few times per minute, you would need really many, many visitors (or maybe some silly flash script that requests the counter at its frame rate )

    Musicman

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

HTML5 Development Center