Flash Site Counter

I seem to be having some trouble setting up a visitor counter on my website. I saw the tutorial by Syko for setting up a counter, I followed his instructions completely but the counter just sits at 1 and does not advance no matter how many times you refresh. The php file doesn’t seem to be saving the new number to the txt file. Can anyone help me out? You can see what I’m talking about on the opening page of my website www.bryancrabtree.com

counter.php script:

<?php
//Reading the file
$count = file_get_contents(“count.txt”);
//Increasing the count
$count = explode("=", $count);
$count[1] = $count[1]+1;
//Writing to file
$file = fopen(“count.txt”, “w+”);
fwrite($file, “count=”.$count[1]);
fclose($file);
//Returning the count
print “count=”.$count[1];
?>

count.txt Contents:

count=0

AS in flash:

this.loadVariables(“counter.php?num=”+random(99));

thanks
Bryan

So what do the lines say…

Yeah I copied it exactly how it was written on the tutorial. There is a different version that I’ll post but I was getting the same error with both. the other script is:

<?php
//Reading the file
$file = fopen(“count.txt”, “r”);
$count = fread($file, 1024);
fclose($file);
//Increasing the count
$count = explode("=", $count);
$count[1] = $count[1]+1;
//Writing to file
$file = fopen(“count.txt”, “w+”);
fwrite($file, “count=”.$count[1]);
fclose($file);
//Returning the count
print “count=”.$count[1];
?>

the only change in the reading file script.

are your permissions on the .txt file 777 to beable to write to it??

<?
$filename = "count.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";
?>

my Permissions are set to 777 on the .txt file. I tried the php script that you posted and that doesn’t work either. I’m really getting frustrated with this. I contacted my server and they said that they are using the latest version of PHP with the latest patches so it should work. I’m about to just give up. I just thought it would be cool to see how many people visit my site, but if it’s going to be such a hastle I might just forget it.

this is a script I found at
http://se2.php.net/manual/sv/function.fopen.php
in the comments section below the article.
try using this instead:


<?php
$counter_file = '/tmp/counter.txt';
clearstatcache();
ignore_user_abort(true);    ## prevent refresh from aborting file operations and hosing file
if (file_exists($counter_file)) {
   $fh = fopen($counter_file, 'r+');
   while(1) {
     if (flock($fh, LOCK_EX)) {
         #$buffer = chop(fgets($fh, 2));
         $buffer = chop(fread($fh, filesize($counter_file)));
         $buffer++;
         rewind($fh);
         fwrite($fh, $buffer);
         fflush($fh);
         ftruncate($fh, ftell($fh));    
         flock($fh, LOCK_UN);
         break;
     }
   }
}
else {
   $fh = fopen($counter_file, 'w+');
   fwrite($fh, "1");
   $buffer="1";
}
fclose($fh);

print "&count=$buffer";

?> 


yeah it’s not working, I’m over it. Thanks for all the help though.

Your PHP script should be


<?php

    //Reading the file
    $count = file_get_contents("count.txt");

    //Increasing the count
    $count = explode("=", $count);
    $count[1] = $count[1]+1;

    //Writing to file
    $file = fopen("count.txt", "w+");
    fwrite($file, "count=".$count[1]);
    fclose($file);

    //Returning the count
    print "&count=".$count[1]."&";  //Notice the '&'....Flash needs this to read any variable from server side scripts
?>

Now for your actionscript…
[AS]
var myCounter = new LoadVars();
myCounter.onLoad = function(success)
{
if (success)
{
trace(this.count); //Outputs the number found (this will not show up when published)
hitsText.text = this.count //adds the count variable into the hitsText text area.
//Any other code goes here
}
else
{
trace(“An error has occured while loading counter.php”); //This can be replaced with any error message you want
}
};
myCounter.load(“counter.php?num=” + random(99), myCounter, “GET”);
[/AS]

If you have any questions, let me know.

Cheers