Users Online
Category: PHPReviewed by: armed rebel
Reviewed on: Jul 05 2006
» Discuss this topic ( Posts)
Here is the entire code to storing data, and I'll go over the parts of it after. You'll be placing this wherever you use your counter.
$sess=session_id(); $now=time(); $check_user_online = mysql_num_rows(mysql_query("SELECT * FROM users_online WHERE sess_id='$sess'")); if($check_user_online<=0) { $create_session=mysql_query("INSERT INTO users_online (sess_id, last_active, ip) VALUES ('$sess', '$now', '$_SERVER[REMOTE_ADDR]')"); } $check_user_online = mysql_num_rows(mysql_query("SELECT * FROM users_online WHERE sess_id='$sess'")); if($check_user_online>=1) { if(isset($_SESSION['user_id'])) { $update_session=mysql_query("UPDATE users_online SET guest='0', last_active='$now', user_id='$_SESSION[user_id]' WHERE sess_id='$sess'"); } else { $update_session=mysql_query("UPDATE users_online SET last_active='$now', guest='1', user_id='NULL' WHERE sess_id='$sess'"); } }
Now letÂ’s go over it. Again, if you feel you have an understanding of the code, skip to the next page.
$sess=session_id();
$now=time();These two simple lines are just setting up for inputting the data in the database. This script relies on sessions being set up, so if you haven't already, add a session_start(); to the top of any page which this counter will be set up. The session_id(); is a unique ID which PHP sets every new session. The time(); is a PHP function that retrieves a UNIX Timestamp that counts the seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
$check_user_online = mysql_num_rows(mysql_query("SELECT * FROM users_online WHERE session_id='$sess'"));
if($check_user_online<=0)
{
$create_session=mysql_query("INSERT INTO users_online (sess_id, last_active, ip_address) VALUES ('$sess', '$now', '$_SERVER[REMOTE_ADDR]')");
}This excerpt checks if your session has been registered and if not, it will add your data. The database row will first register the user as a guest (remember, the guest column defaults to 1), then, in the next step, it will update it to whether or not your user is a member.
$check_user_online = mysql_num_rows(mysql_query("SELECT * FROM users_online WHERE sess_id='$sess'"));
if($check_user_online>=1)
{
if(isset($_SESSION['user_id']))
{
$update_session=mysql_query("UPDATE users_online SET guest='0', last_active='$now', user_id='$_SESSION[user_id]' WHERE session_id='$sess'");
}
else
{
$update_session=mysql_query("UPDATE users_online SET last_active='$now', guest='1', user_id=NULL WHERE sess_id='$sess'");
}
}This part updates the users_online table and updates it to whether you are a member or guest. The reason we query the database again, is because we are checking the data you may have just added, and without it would take two page loads to display a registered member on your site. You may change whatever session variable you are using that only a member would have, but for our use, it'll just be user_id. The bulk of this step is self explanatory so we'll move on to the next step.

