Solved

Monitor logged in users

Posted on 2004-09-15
4
301 Views
Last Modified: 2011-10-03
I'd like to know if there is a way to monitor what users are logged in on my website at any given time.  How do I monitor the sessionIDs the server is assigning for each logged in user?  My website is hosted by Powweb.com
0
Comment
Question by:GregArtemides
  • 3
4 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12062931
I have convered this one fairly well before here: http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21063033.html

If your users arent logged in with a username you could use something like their IP instead ($_SERVER['REMOTE_ADDR']; )
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 250 total points
ID: 12062958
For example

you need a database called "whosonline" with two fields "timestamp" (int(15) should do) and "user_ip" (varchar(20))

the include file (to be included in every page)

 $timeoutseconds = 120; //the amount of time in seconds that you want the user to time out at with no activity
 $timestamp = time();
 $timeout = $timestamp-$timeoutseconds;

 mysql_query("INSERT INTO whosonline VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."')") or die("insert error ".mysql_error());
 mysql_query("DELETE FROM whosonline WHERE timestamp<'$timeout'") or die("delete error ".mysql_error());


Then to count the users online on any given page

$query = mysql_query("SELECT DISTINCT user_ip FROM whosonline") or die(mysql_error());
$qcount = mysql_num_rows($query);

//number of users online is stored in $qcount

To display some information:

<?php
for($i=0;$i<$query_count;$i++) {
 $user_ip = mysql_result($query,$i,"user_ip");
 echo $user_ip."<br>\n";
}
?>

The above will list the IP's of active users. You would probably be better off working with a session value as shown in the other thread but the above is the general idea.
0
 
LVL 1

Author Comment

by:GregArtemides
ID: 12063293
Thanks Diablo84.  I see your logic.  Shouldn't be hard to implement on my system since I'm already using a session variable 'username'.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12063354
Good to hear, if you run into any trouble getting it working along the way please feel free to post back here and il see if i can assist you.

Good luck, |)iablo
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question