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

10.07.2011
| 1626 views |
  • submit to reddit
        <?php
session_start();
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="http://www.twizap.com/game/tank-trouble-3.html">Tank Trouble 3</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.
    if(!$row){
        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;
?>