Dynamic Flash Part 1: Counting Users Online - Whose Online Using Flash
(Page 3 of 5 )
You may have remembered a while back when we published an article entitled 'Finding How Many Users Are On Your Site With PHP'. We will be modifying this code to suit our needs.
Basically, when the code runs it will check to see if an entry for your I.P. address is already in the database and add an entry if it's not. It then counts how many distinct users (based on their I.P. address) have been on your site within the last 20 mins and spits that number out.
This time, we will start with our PHP script and MySQL database. Fire up the MySQL console window and create a database called usersOnline:
create database usersOnline;
use usersOnline; Add a table to the database with the following query:
create table usersOnline
(
id int auto_increment not null,
userIP varchar(20) not null,
dateAdded timestamp,
primary key(id),
unique id(id)
); This will enable us to track our users. Next, create a new PHP file called count.php. Add the following code to count.php:
<?php
$dbServer = "localhost";
$dbName = "usersOnline";
$dbUser = "root";
$dbpass = "";
//add user to database
global $HTTP_SERVER_VARS;
define("SESSION_LENGTH", 20);
$userIP = $HTTP_SERVER_VARS["REMOTE_ADDR"];
$sConn = @mysql_connect($dbServer, $dbUser, $dbPass);
$dbConn = @mysql_select_db($dbName, $sConn);
$timeMax = time() - (60 * SESSION_LENGTH);
$result = @mysql_query("select count(*) from usersOnline where unix_timestamp(dateAdded) >= '$timeMax' and userIP = '$userIP'");
$recordExists = mysql_result($result, 0, 0) > 0 ? true : false;
if(!$recordExists)
{
// Add a record for this user
@mysql_query("insert into usersOnline(userIP) values('$userIP')");
}
//count users online
$result = mysql_query("select count(*) from usersOnline where unix_timestamp(dateAdded) >= '$timeMax'");
$usersOnline = mysql_result($result, 0, 0);
//create our flash variable
echo 'Count=' . $usersOnline;
?> Place it on your server in a folder called count. When run, the script will output something like this:
Count=1Next: Whose Online Using Flash (contd.) >>
More Flash Articles
More By Aaron Schaap