Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'whos online'

Status
Not open for further replies.

BigBadDave

Programmer
May 31, 2001
1,069
0
0
EU
How would I do a simple 'whos online' thing?

I presume I would add a users IP and Date/Time to MYSQL

Then if another user comes online the page would run a query to delete all rows greater than say 5 mins old

Or can I do some sort of onexit code to remove a user form MYSQL?

Any thoughts Regards

David Byng

spider.gif


davidbyng@hotmail.com
 
Don't use IP addresses, as many people have them dynamically allocated each time the connect to the internet.

If you want to know who's online at your site then you should have their information (login id) to use as a unique reference. You could store their info on your site without passwords and a cookie, but what if the cookie gets deleted?

Keep their info and password it so they can alter the info if they want. Signon script not only logs them in but registers them as being on the site. Logging out does the opposite and also include a limit on the amount of time they are online before logging them out - for security. A page refresh will log the time or log them out if over the allocated time. You can then refresh the list of people who are logged in using the time as the boundary. This will work around a persons computer crashing without logging out.

Hope this helps as a guideline.
 
On the main page I do an update on the user record. I save the current datetime off the user who is logged in. (ea. the username in the cookie)

I also do an select on the userlist where all records are selected where the datetime is within the last half hour
beneath here both queries are mentioned and they are both on the main page


$hourdiff = "2";
$timeadjust = ($hourdiff *60 * 60);
$datum = gmdate("Y-m-d G:i:s",time() + $timeadjust);

$query="UPDATE lowlandusers SET llulastonforum='$datum' WHERE lluid='$id'";
$rs3=mysql_query($query,$conn);


function Opforum($conn){
$hourdiff = "2";
$houradjust = ($hourdiff *60 * 60);
$minutediff = "30";
$minuteadjust = ($minutediff * 60);
$datum = gmdate("Y-m-d G:i:s",time() - $minuteadjust + $houradjust);

$query="SELECT lluname,date_format(llulastonforum,'%Y-%m-%d %H:%i:%s') as datumlaatst FROM lowlandusers order by llulastonforum desc";
$rs2=mysql_query($query,$conn);
$list = mysql_num_rows($rs2);

while($i < $list)
{
$row = mysql_fetch_array($rs2);
$naamuser=$row[&quot;lluname&quot;];
$datumlaatst=$row[&quot;datumlaatst&quot;];
if ($datumlaatst > $datum) {
$test=strtotime($datumlaatst);
$test2=strtotime($datum);
$test3=$test - $test2;
$tijd=$minutediff - ($test-$test2)/60;
if ($tijd >=&quot;0&quot; AND $tijd < &quot;2&quot;) {$kleurcode=&quot;33ff66&quot;;}
if ($tijd >=2 AND $tijd < 5) {$kleurcode=&quot;33cc66&quot;;}
if ($tijd >=5 AND $tijd < 10) {$kleurcode=&quot;333366&quot;;}
if ($tijd >=10 AND $tijd < 20) {$kleurcode=&quot;006666&quot;;}
if ($tijd >=20 AND $tijd < 30) {$kleurcode=&quot;003300&quot;;}
print &quot;<font color=$kleurcode>$naamuser </font>&quot;;
}
$i++;
}

}
 
Just keep in mind that since HTTP is stateless, any attempt to show a list of current users is at best a guess. ______________________________________________________________________
Did you know?
The quality of our answers is directly proportional to the number of stars you vote?
 
a small thingy - you may wanna use a Heap table in whatever database you'r running, since this table will be queried/updated/deleted often, a heap table will deliver better performance and save you a little I/O wait time.

take into account that heap tables can't use primary indexes, or keep strings, so storing an IP address (just for example) should be done by multiplying each segment by a 3rd power of 10 ... to change 1.2.3.4 into 100200300400.
 
I will tell you one thing. The best thing you can do to have an application of &quot;who's online&quot;, you should do your own session control system.


With this you can manage your sessions and then see who's online in your site. I suggest you to do this in database, and you should store session_id, session_data, session last change, username and login date.

mainly you have to write 6 functions:
open (save_path, session_name) - What to do when you open a session. For example, connect to database
close - What to do when you close a session. For example, close the database
read(id) - What to do when you read session information. Read data from database
write(id,sess_data) - what to do when to update data. Insert or update database
destroy (id) - When you destroy the session. Delete from the database
gc(maxlifetime) - Garbage collect. Delete from the database all the records not changed in the last maxlifetime seconds.

Then, to see who's online, just &quot;select distinct username from sessions&quot;

I already have done that session control thing for MSSQL, and it is simple code.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top