Hi,
I have a question. How difficult would it be to add a section to my site where people can see how many guests are currently viewing the site (show a number) using actionscript 3.0 and possibly PHP?
Thanks,
fs_tigre
Hi,
I have a question. How difficult would it be to add a section to my site where people can see how many guests are currently viewing the site (show a number) using actionscript 3.0 and possibly PHP?
Thanks,
fs_tigre
In php you need to know when an user is active or has left the website and then tell flash how many users there are using an xml. Maybe using cookies, or global vars. The as3 part should be only reading the xml and displaying the data.
Easiest way would be to use a database. The user has a unique ID that is sent to a PHP script.
If the ID is in the database, it updates a “last seen” time column. If they are not in there, it adds an entry for their unique ID.
Every so often, one of those requests would also run a “cleanup” where entries not seen in over X minutes would be removed.
Then, in order to determine your number of active visitors, just do a count on the table and send the result to Flash via XML. Unless you used a socket server, you’d have to poll the server every so many minutes - not ideal, but it’d work.
Most of what you’re after requires server-side scripting (eg. PHP) and SQL knowledege. The AS3 side of things is trivial in comparison.
So, this wouldn’t be possible without having a user ID? Again what I want to show is just the number of visitors currently viewing the page.
Thanks
You don’t need a user ID as far as a “login” (ie. username and password) is concerned - you would just generate a unique ID for each anonymous visitor. This ID would be destroyed some time after the user “leaves” (ie. times out or stops viewing) the site.
Flash must interact with the server for this information. The server must therefore store this information. So you must use some kind of serverside logic to tell Flash how many people are visiting, even if you just want a simple number to be displayed.
Thanks, I will try to find a tutorial.
Create dynamic text fiels
instance name
my_out_txt
//start code first frame
var url:String = "count_attendance.php";
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.exampleSessionId = new Date().getTime();
variables.user_name = "";
variables.user_message = "";
request.data = variables;
var loader:URLLoader = new URLLoader();
configureListeners(loader);
try {
loader.load(request);
}
catch (error:Error)
{
trace("Unable to load requested document.");
}
function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
}
function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace("completeHandler: " + loader.data);
//trace("completeHandler: " + loader.data.GaleryScoresText);
var vars:URLVariables = new URLVariables(loader.data);
trace("The GaleryScoresText is " + vars.buff);
my_out_txt.text=vars.HighScoresText;
}
Create
php-file count_attendance.php and
txt-file count_attendance.txt
in current hosting path swf-file
<?php
//Содержимое count_attendance.php
//чтение-запись (режим r+)
$visit="";
$fh=fopen(“count_attendance.txt”,“r+”);//read and write mode
flock($fh,LOCK_EX);//lock
$visit=fread($fh,10);//read 10 byte
fseek($fh,0,0);//in the begin
$visit=$visit+1;
fputs($fh,$visit);//write data form filee
flock($fh,LOCK_UN);//un lock
fclose($fh);//close file
//swf catches fish
echo “HighScoresText=”.$visit;////////////////////////////////////////////////////////////////////////////
?>
Eugh, using text files and locking things - bad, bad, bad. Leads to too many concurrency issues. Better to use a database. Anything like trying to replicate database functionality using a text file will end up being a kludge.
:: Copyright KIRUPA 2024 //--