DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Number Users Currently Online

  • submit to reddit
include"db.php"; # Connect To the database
$active_sessions = 0;
$minutes = 5; # period considered active
if($sid = session_id()) # if there is an active session
    # DB connect here
    $ip = $_SERVER['REMOTE_ADDR']; # Get Users IP address
    # Delete users from the table if time is greater than $minutes
    mysql_query("DELETE FROM `active_sessions` WHERE 
    `date` < DATE_SUB(NOW(),INTERVAL $minutes MINUTE)")or die(mysql_error()); 
    <a href="">Tank Trouble</a>
    # Check to see if the current ip is in the table
    $sql = mysql_query("SELECT * FROM active_sessions WHERE ip='$ip'");
    $row = mysql_fetch_array($sql);
    # If the ip isn't in the table add it.
        mysql_query("INSERT INTO `active_sessions` (`ip`, `session`, `date`) 
        VALUES ('$ip', '$sid', NOW()) ON DUPLICATE KEY UPDATE `date` = NOW()")or die(mysql_error());
    # Get all the session in the table
    $sessions = mysql_query('SELECT * FROM `active_sessions`')or die(mysql_error());
    # Add up all the rows returned
    $active_sessions = mysql_num_rows($sessions);
    # Print the final result
    echo'<b>Online Now: </b>'.$active_sessions;