# Flash Site Counter

**URL:** <https://forum.kirupa.com/t/flash-site-counter/41902>\
**Category:** flash\
**Created:** [February 3, 2004, 7:02am UTC](https://forum.kirupa.com/t/flash-site-counter/41902 "2004-02-03T07:02:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcrabtree](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@bcrabtree](https://forum.kirupa.com/u/bcrabtree)\
**Post date:** [February 3, 2004, 7:02am UTC](https://forum.kirupa.com/t/flash-site-counter/41902/1 "2004-02-03T07:02:35Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 3, 2004, 11:14pm UTC](https://forum.kirupa.com/t/flash-site-counter/41902/2 "2004-02-03T23:14:41Z")

</div>

So what do the lines say…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 3, 2004, 11:16pm UTC](https://forum.kirupa.com/t/flash-site-counter/41902/3 "2004-02-03T23:16:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 3, 2004, 11:41pm UTC](https://forum.kirupa.com/t/flash-site-counter/41902/4 "2004-02-03T23:41:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 3, 2004, 11:45pm UTC](https://forum.kirupa.com/t/flash-site-counter/41902/5 "2004-02-03T23:45:43Z")

</div>

```php
<?
$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";
?>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 4, 2004, 12:36am UTC](https://forum.kirupa.com/t/flash-site-counter/41902/6 "2004-02-04T00:36:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 4, 2004, 7:56am UTC](https://forum.kirupa.com/t/flash-site-counter/41902/7 "2004-02-04T07:56:27Z")

</div>

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

```php

<?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";

?> 

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 4, 2004, 11:51am UTC](https://forum.kirupa.com/t/flash-site-counter/41902/8 "2004-02-04T11:51:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 1:33am UTC](https://forum.kirupa.com/t/flash-site-counter/41902/9 "2004-02-06T01:33:54Z")

</div>

Your PHP script should be

```php

<?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
