[PHP] [WITH SOURCE] Simple Online Users Script
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 Code:
<?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 Code:
<?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 Code:
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: www.mark-eriksson.com/projects/PHPOnlineUsers
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.