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];
?>
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];
?>
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.
<?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]