Location: PHPKode > scripts > Users online > users-online/online.php
<?php
/*
* version: 2.0.1
* copyright (c) 2009 spyka Web Group
* license http://opensource.org/licenses/gpl-license.php GNU Public License
*/


$db['h'] = 'localhost';        // MySQL host - usually localhost, but for some hosts it may be different
			       // contact your hosting provider if you are unsure

$db['u'] = 'user';             // Username for the MySQL account being used

$db['p'] = 'pass';             // Password for above username

$db['d'] = 'database';         // Name of database you have already created. Doesn't have to be empty (only 1 table will be added)
                               // You should create this manually via phpMyAdmin or whatever system your web host has setup to allow  
			       // you to add databases

define('UO_TIMEOUT', 300);     // Change the 300 here to change how long a user is counted online for. Value is in seconds. 300 = 5 minutes


/* ADVANCED OPTIONS */	
/* Only change these if you really need to and know what you're doing */

define('UO_TABLE', 'usersonline');    // Name of mySQL table

define('UO_MOSTONLINE', 'uo_record'); // Name of record for the most online record
	
////////////////////////////////////////////   DO NOT EDIT BELOW HERE	////////////////////////////////////////////

$link =	@mysql_connect($db['h'], $db['u'], $db['p']);
$dbl  =	@mysql_select_db($db['d'], $link);

unset($db);
	
function users_online()
{
	global $link;
		  
	if(!$link)
	{
		die('Error in selecting database');
	}
	
	$id			= mysql_real_escape_string(md5($_SERVER['REMOTE_ADDR']), $link);
	$time_now	= (int) time();
	
	$exist_check = mysql_query("SELECT `online` FROM `" . UO_TABLE . "` 
								WHERE `id` = '{$id}'", $link);
	
	if(mysql_num_rows($exist_check) >= 1)
	{
		$update = mysql_query("UPDATE `" . UO_TABLE . "`
								SET `online` = '{$time_now}' 
								WHERE `id` = '{$id}'", $link);
	}
	else
	{
		$insert = mysql_query("INSERT INTO `" . UO_TABLE . "` 
								(`id`, `online`) 
								VALUES('{$id}', '{$time_now}')", $link);
	}
	
	$deletewhere =  (int) ($time_now - UO_TIMEOUT);

	
	$delete = mysql_query("DELETE FROM `" . UO_TABLE . "` 
							WHERE `online` < '{$deletewhere}'
								AND `online` <> -1", $link);
	
	$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UO_TABLE . "` 
										WHERE `online` <> -1", $link), 0);
	
	
	
	$record = mysql_result(mysql_query("SELECT `id` FROM `" . UO_TABLE . "` 
										WHERE `online` = -1 
											AND `id` LIKE '" . UO_MOSTONLINE . ":%'", $link), 0);
											
	list($varname, $record) = split(':', $record);
	
	if($count > $record)
	{
		mysql_query("UPDATE `" . UO_TABLE . "`
					SET `id` = CONCAT('" . UO_MOSTONLINE . "', ':', {$count})
					WHERE `id` = '{$varname}:{$record}'", $link);
		$record = $count;
	}
	
	return array($count, $record);
	
}	


?>
Return current item: Users online