• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[PHP] [WITH SOURCE] Simple Online Users Script

Joined
Oct 24, 2009
Messages
536
Reaction score
217
This took me 20 minutes to code, I made it a few weeks ago but I've only just been bothered to release it.

You can either download the source codes by clicking here (password is m0nstadot).. or you can save the files manually from below.

Don't forget to change your MySQL details in both of the PHP files.

config.php
PHP:
<?php
/*
PHPOnlineUsers script by m0nsta. (Mark Eriksson)
http://www.mark-eriksson.com
*/
session_start();

mysql_connect( "localhost", "dbuser", "dbpass" ); //connect to DB
mysql_select_db( "dbname" ); //select DB

$file_viewed = $_SERVER['REQUEST_URI']; //get the page they have visited
$ip = $_SERVER['REMOTE_ADDR']; //get their ip address
$time = time(); //get the time they loaded the page
$five_mins = $time - 60; //5 minute interval
$session = session_id(); //get their unique session id

$checkQ = mysql_query( "SELECT * FROM onlineusers WHERE `session` = '$session'" );
if( mysql_num_rows( $checkQ ) > 0 )
{
	mysql_query( "DELETE FROM `onlineusers` WHERE `session` = '$session'" );
}
mysql_query( "INSERT INTO `onlineusers` (`ip`, `time`, `page`, `session`) VALUES ('$ip', '$time', '$file_viewed', '$session');" );
mysql_query( "DELETE FROM `onlineusers` WHERE `time` < '$five_mins'" ); //remove all sessions in table that have been inactive since 5 minutes

$online_users = mysql_num_rows( mysql_query( "SELECT * FROM `onlineusers`" ) ); //get the total amount of online users
?>

index.php
PHP:
<?php
/*
PHPOnlineUsers script by m0nsta. (Mark Eriksson)
http://www.mark-eriksson.com
*/
include( "config.php" );
session_start();

mysql_connect( "localhost", "dbuser", "dbpass" ); //connect to DB
mysql_select_db( "dbname" ); //select DB

$onlineQ = mysql_query( "SELECT * FROM onlineusers" );
if( mysql_num_rows( $onlineQ ) == 0 )
{
	echo "No one is online!";
}
else
{
	echo "<style type=\"text/css\">th,td{border:1px;}</style><table>
		<table>
			<tr>
				<th>IP</th>
				<th>Time</th>
				<th>Page</th>
				<th>Session</th>
			</tr>";
	while( $onlineA = mysql_fetch_array( $onlineQ ) )
	{
		$ip = $onlineA["ip"];
		$time = date( "F jS Y - g:i a", $onlineA["time"] );
		$page = $onlineA["page"];
		$session = $onlineA["session"];
		echo "<tr>
			<td>{$ip}</td>
			<td>{$time}</td>
			<td>{$page}</td>
			<td>{$session}</td>
		</tr>";
	}
	echo "</table>";
}
?>

run this SQL query
PHP:
CREATE TABLE IF NOT EXISTS `onlineusers` (
  `ip` varchar(100) NOT NULL,
  `page` varchar(350) NOT NULL,
  `time` varchar(100) NOT NULL,
  `session` varchar(1000) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

LIVE DEMO:

This will show a users IP Address, the page they visited, the time they visited and their unique session ID.

Leave credits if you want, it's up to you.

Hope you like it blah blah.

Thanks,
- m0nsta.
 
Last edited:
Back
Top