Be a Supporter!

As: Flash/php Webpage Hit Counter

  • 2,908 Views
  • 8 Replies
New Topic Respond to this Topic
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
As: Flash/php Webpage Hit Counter 2006-04-06 05:58:55 Reply

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
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Flash/php Webpage Hit Counter 2006-04-06 06:00:38 Reply

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
Vengeance
Vengeance
  • Member since: Mar. 18, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to As: Flash/php Webpage Hit Counter 2006-04-06 06:07:45 Reply

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
shazwoogle
shazwoogle
  • Member since: Sep. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Flash/php Webpage Hit Counter 2006-04-06 07:08:08 Reply

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

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Flash/php Webpage Hit Counter 2006-04-06 07:23:59 Reply

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
shazwoogle
shazwoogle
  • Member since: Sep. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Flash/php Webpage Hit Counter 2006-04-06 07:30:55 Reply

AS: Flash & PHP by shazwoogle

Awsome :D thanks

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Flash/php Webpage Hit Counter 2006-04-06 07:52:22 Reply

one other way to do this is Armor Bot

Katocan
Katocan
  • Member since: May. 24, 2004
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Flash/php Webpage Hit Counter 2006-04-06 13:22:45 Reply

pretty cool.

xXNetoXx
xXNetoXx
  • Member since: Jul. 19, 2005
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As: Flash/php Webpage Hit Counter 2006-12-01 14:45:43 Reply

it looks great.

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

any idea???