AskBee.NET
Google
AskBee - Articles, Tutorials.
   Categories    Resources
   Photo Gallery    Contact

Categories / php / Whos online

By Bee
Last updated: December 18, 2005
 

Always know who is browsing your website, who is online

 

I know you always wanted to know who is browsing your website and how many users are online. This one is an easy script, and basically the code is inspired from oscommerce.

Here is the table structure for online users:

Online Users

I will use PEAR's DB_DataObject as database layer in this tutorial. If you are not familiar with DB_DataObject you can read about it here. I will consider that database connection settings has already been set, as well as dataobject files.

This structure is a simpler one, as you can see we only record Session ID, IP Address, time the user enters your website, last time the user clicked a link on your website.
You could also add some extra information about online users like: what page are they browsing, browser type, where are they coming from, etc.

We want to know who is online in the last 15 minutes. A unique Session ID is generated automatically, and we will store that ID in our database.

Insert or update a user's information into online_users table

//create online_users object
$online_users = DB_DataObject::factory( 'online_users' );
//check if a user has already visited your website in the last 15 minutes.
$nr = $online_users->get('session_id', session_id() );
//user has already visited your website, we make an update of time_last_click
if( $nr > 0 )
{
$online_users->time_last_click = time();
$online_users->update();
}
//user visits your website for the first time in the last 15 minutes, we make an insert
else
{
$online_users = DB_DataObject::factory( 'online_users' );
$online_users->time_last_click = time();
$online_users->time_entry = time();
$online_users->ip_address = $_SERVER['REMOTE_ADDR'];
$online_users->insert();
}

The next thing to do is do delete from our table the users that have time_last_click greater than 15 minutes.

//create online_users object
$online_users = DB_DataObject::factory( 'online_users' );
$online_users->whereAdd("time_last_click < '" . time() - 900 . "'");
$online_users->delete(DB_DATAOBJECT_WHEREADD_ONLY);

These two code snippets should be included in every page of your website.

And the last piece of code, we will list online users:

//create online_users object
$online_users = DB_DataObject::factory( 'online_users' );
$online_users->orderBy("time_last_click DESC");
$online_users->find();
while( $online_users->fetch() )
{
echo "IP:" . $online_users->ip_address . " --- Entry time: " . date('H:i:s', $online_users->time_entry ) . '<BR>';
}

Find out more about online users.

Advertising
Are you paying more than $170 per month on your credit cards?
Let us help you. Free Debt Relief !
Host your website now
Unlimited Hosting
Cheap Hosting
Bee recommends:
SEO Articles
AskBee Hosting Plans
Affordable hosting
AskBEE Directory
www.codeworkshq.com
www.averagedesign.com
www.4invent.com
Catering
Anunturi online
revelion 2009
Privacy Policy | Top Searches | Cheap web hosting | Ringtomes | Shared Hosting Valid HTML 4.01 Transitional Valid CSS!