00:00
00:00

As: Flash/php Webpage Hit Counter

3,650 Views | 8 Replies
New Topic Respond to this Topic

As: Flash/php Webpage Hit Counter 2006-04-06 05:58:55


AS: Main

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


- - Flash - Music - Images - -

BBS Signature

Response to As: Flash/php Webpage Hit Counter 2006-04-06 06:00:38


Couple of AS lines got broken

loadC.load ("http://sitename.net/counter.php?fcook=1"
);
loadC.load ("http://sitename.net/counter.php?fcook=0"
);

Just move the closing parenthesis back onto the end of the previous line and all should be fine


- - Flash - Music - Images - -

BBS Signature

Response to As: Flash/php Webpage Hit Counter 2006-04-06 06:07:45


very nice chunks of text there. you just didn't explain too much.
oh well, it's for the more advanced actionscripters/programmers so they'll be able to figure it out themselves.

<3 <3

========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

BBS Signature

Response to As: Flash/php Webpage Hit Counter 2006-04-06 07:08:08


nice you should have put a link to my php tutorial?

Response to As: Flash/php Webpage Hit Counter 2006-04-06 07:23:59


At 4/6/06 06:07 AM, -Vengeance- wrote: very nice chunks of text there. you just didn't explain too much.

It's never been my intention to explain too much in AS threads, check back to the early pages of AS Main. I'm not going to discourage others from fully commenting/explaining their code, but I personally believe that a good way of learning is to take a lump of code and play with it to find out how each bit works. That's how I learnt.
Also, there are a few commented lines here and there both in the php and AS code lumps, which give the basic idea.

oh well, it's for the more advanced actionscripters/programmers so they'll be able to figure it out themselves.

Not really, anyone can take these codes and use them as is. No skill is required for that. Anybody who wants to adapt them to suit their own needs will have to spend a little time understanding how it works, though.

At 4/6/06 07:08 AM, shazwoogle wrote: nice you should have put a link to my php tutorial?

AS: Flash & PHP by shazwoogle

Also:
AS: Arrays
AS: Save and Load (Flash cookies)
AS: Loading/Unloading Data by shazwoogle


- - Flash - Music - Images - -

BBS Signature

Response to As: Flash/php Webpage Hit Counter 2006-04-06 07:30:55


AS: Flash & PHP by shazwoogle

Awsome :D thanks

Response to As: Flash/php Webpage Hit Counter 2006-04-06 07:52:22


one other way to do this is Armor Bot

Response to As: Flash/php Webpage Hit Counter 2006-04-06 13:22:45


pretty cool.

Response to As: Flash/php Webpage Hit Counter 2006-12-01 14:45:43


it looks great.

Im trying to do a flash survey with a similar form, but i just can't

any idea???