U.S. Department of the Navy

General Category => U.S. Fleet Cyber Command => Topic started by: DeptNav on January 15, 2014, 08:27:38 am


Title: [PHP] Online Users Script
Post by: DeptNav on January 15, 2014, 08:27:38 am
Credit to darren.

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

config.php


Code: [Select]
<?php/*PHPOnlineUsers script by Darren (HairyWhale)hqaf-dod.freeforums.net*/session_start(); mysql_connect( "localhost", "dbuser", "dbpass" ); //connect to DBmysql_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


Code: [Select]
<?php/*PHPOnlineUsers script by Darren (HairyWhale)hqaf-dod.freeforums.net*/include( "config.php" );session_start(); mysql_connect( "localhost", "dbuser", "dbpass" ); //connect to DBmysql_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


Code: [Select]
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; 
This will show a user's IP Address, the page they visited, the time they visited and their unique session ID.