Not much AS involved in this, but I figured it would be useful to some people.
You can see this in action at the bottom of the home mainframe on my magnets page.
For this to work, you'll need a host that supports PHP
===============================
The TEXTFILE
This will hold the hits, since I can't be bothered with databases
Open a new textfile in Notepad
Type this:
0---0
The first digit will hold total page hits. The second will hold unique hits.
Save the file as 'counter.txt'
The PHP
Open a new file in Notepad or the PHP editor of your choice.
Paste this:
<?php
$thefile= "counter.txt";
$thecount= file_get_contents ($thefile);
//Convert to array
$thecount= explode("---", $thecount);
$total= $thecount[0]; $unique= $thecount[1];
if($fcook<>""){
//If Flash cookie is not present, add one to the unique counter
if($fcook<1){ $unique=$unique+1;}
}
//Add one to total hits
$total=$total+1;
$output=$total."---".$unique;
//Write to page for Flash to read
echo $output;
//Open the textfile, write new count, close it
$fp= fopen($thefile, "w"); fputs($fp, $output); fclose($fp);
?>
This file should be saved as 'counter.php', and belongs in the same folder as counter.txt
The FLASH
New Flash document.
Draw two dynamic textboxes. One should have the var 'totl', and the other should have the var 'uniq'
On the first (and only) frame of the fla, add these actions:
//Set up the LoadVars
loadC=new LoadVars();
loadC.onData= function(raw){
arr=raw.split("---"); //Convert input to array
totl="TOTAL HITS: "+arr[0];
uniq="UNIQUE VISITS: "+arr[1];
}
var sfile = SharedObject.getLocal ("dnmagnets");
if (sfile.data.score == undefined) { //If page has NOT been viewed before
loadC.load ("http://sitename.net/counter.php?fcook=0"
);
sfile.data.score = 0;sfile.flush();
}else{ //If page HAS been viewed before on this PC
loadC.load ("http://sitename.net/counter.php?fcook=1"
);
}
In this code, you will need to replace 'http://sitename.net' with the path to the counter.php file on your server. Bear in mind that if you use a relative path, you need it to be relative to the HTML page in which the swf is embedded, not to the swf itself.
FINAL
Embed your swf into an HTML page of your choice. Upload all files.
===================================
Total hits will unfortunately go up every time you refresh the page - can't do much about that. Unique hits will record only the first time a certain PC accesses the page: a Flash cookie will then be placed on that machine, so it won't register as a unique hit the next time it loads the page.
Feel free to suggest adaptations/customisations