# How many people are currently viewing my site?

**URL:** <https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315>\
**Category:** flash\
**Created:** [June 14, 2008, 12:14pm UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315 "2008-06-14T12:14:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fs\_tigre](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@fs\_tigre](https://forum.kirupa.com/u/fs_tigre)\
**Post date:** [June 14, 2008, 12:14pm UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315/1 "2008-06-14T12:14:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Pier25](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pier25](https://forum.kirupa.com/u/Pier25)\
**Post date:** [June 14, 2008, 12:46pm UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315/2 "2008-06-14T12:46:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 15, 2008, 7:01am UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315/3 "2008-06-15T07:01:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fs\_tigre](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@fs\_tigre](https://forum.kirupa.com/u/fs_tigre)\
**Post date:** [June 17, 2008, 2:06pm UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315/4 "2008-06-17T14:06:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 17, 2008, 3:14pm UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315/5 "2008-06-17T15:14:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fs\_tigre](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@fs\_tigre](https://forum.kirupa.com/u/fs_tigre)\
**Post date:** [June 18, 2008, 4:06pm UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315/6 "2008-06-18T16:06:39Z")

</div>

Thanks, I will try to find a tutorial.

---

<div class="post-metadata">

**Author:** ![Alex\_Lexcuk](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Alex\_Lexcuk](https://forum.kirupa.com/u/Alex_Lexcuk)\
**Post date:** [June 18, 2008, 5:06pm UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315/7 "2008-06-18T17:06:16Z")

</div>

Create dynamic text fiels  
instance name  
my\_out\_txt

```auto

//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;////////////////////////////////////////////////////////////////////////////  
?\>

[http://murmadillo.tut.su/php\_flash/page\_0.html](http://murmadillo.tut.su/php_flash/page_0.html)

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 19, 2008, 4:38am UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315/8 "2008-06-19T04:38:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alex\_Lexcuk](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Alex\_Lexcuk](https://forum.kirupa.com/u/Alex_Lexcuk)\
**Post date:** [June 19, 2008, 8:24am UTC](https://forum.kirupa.com/t/how-many-people-are-currently-viewing-my-site/263315/9 "2008-06-19T08:24:01Z")

</div>

[http://cnstats.com/](http://cnstats.com/)
