LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2009, 12:59 PM   #1
gogoabc
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Rep: Reputation: 0
How to get real time information about how many user are logged in and their ips


I use tomcat as my server platform in Ubuntu for a war file.

I know in order to get real time information about how many user are logged in, we can count how many active sessions exist by a SessionCounter code. However, I have to permit HttpSessionListener in web.xml of tomcat. From other users' experiences, the configuration is complexed and has some errors.

Here's the link:
http://www.stardeveloper.com/article...1112001&page=1

In order to get users' ip, in jsp, use request.getremotehost() or
request.getremoteaddress() by editing the jsp file.

I wonder if there's some open source software to use for these two purposes.

Thank you!

Last edited by gogoabc; 01-20-2009 at 02:11 PM.
 
Old 01-21-2009, 12:55 AM   #2
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Rep: Reputation: 103Reputation: 103
Not sure if applicable to your situation at all, but I got this code off the net to use with PHP and MySQL to count, within the last minute, all unique users on a website. Maybe it can give you an idea or you can adapt it.

First, the MySQL table required:

Code:
CREATE TABLE `poweralert3_users_online` (
  `timestamp` int(15) NOT NULL default '0',
  `ip` varchar(40) NOT NULL default '0',
  PRIMARY KEY  (`ip`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
and then this code is used to determine the count, and other code (not shown here) displays at the bottom of the page "nnn users online" where nnn is the number of unique IPs:

Code:
function getUsersOnline() {
   $server = $_SESSION['hostname'];
    $database = $_SESSION['db_name'];
    $user = $_SESSION['username'];
    $pw = $_SESSION['db_password'];

    // connect to the database
    $connection = @mysql_connect ($server, $user, $pw) or die ("Error connecting to the database.");

    // select the database
    $db = @mysql_select_db ($database) or die ("Error selecting database.");

    // time limit for users online in seconds - you can increase or decrease this if you want
    // recommended: entries in db older than 1 minute will be removed
    $timeout = 60;

    // get the ip address
    $ip = getenv("REMOTE_ADDR");

    // get the time
    $timestamp = time();
    $timelimit = $timestamp - $timeout;

    // delete expired users
    $sql = "DELETE FROM " . $_SESSION['cms_db_prefix'] ."users_online WHERE timestamp < $timelimit";
    $result = @mysql_query ($sql);

    // insert current user
    $sql = "INSERT INTO " . $_SESSION['cms_db_prefix'] ."users_online (timestamp, ip) VALUES ('$timestamp', '$ip')";
    $result = mysql_query ($sql);

    // if ip already exists, update the timestamp
    if (mysql_errno() == 1062) {
        $sql = "UPDATE " . $_SESSION['cms_db_prefix'] ."users_online SET timestamp = '$timestamp' WHERE ip = '$ip'";
        $result = mysql_query ($sql);
    }

    // get number of users online
    $sql = "SELECT COUNT(*) as num_users FROM " . $_SESSION['cms_db_prefix'] ."users_online";
    $result = mysql_query ($sql);
    $row = mysql_fetch_array($result);
    $num_users = $row['num_users'];
    
    if ($num_users == 1)
        return "1 user";
    else
        return $num_users . " users";
}
Hope this helps or at least gives you an idea of how you might approach the problem in your environment.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need some detail information on Hard Real-Time Systems and Soft real-Time Systems. LinuxInfo General 3 09-22-2008 03:25 AM
Need Information regarding real time embedded linux operating system insha Linux - Newbie 2 06-12-2007 07:36 AM
Can anyone give me information on Real Time Embedded Linux? insha Linux - General 1 06-12-2007 05:42 AM
Can I limit time a user can be logged in ? glenn69 Linux - General 2 01-15-2007 09:26 PM
Remote message: User: already logged onto system 1 time(s). jamaso Linux - General 1 11-08-2002 05:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration