Who is online ??

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Who is online ??

Post by winsonlee »

I would like to know how am i able to design a system in such a way that the system knows which user is logon to the system ?? How does the structure of the mysql database ? is there a table to keep track of all the user who is login to the system ?? When a user login to the system, does the username get inserted in a table ?? If this is the case, than when a user close a browser, how the system knows how to remove the name from the table ??

is there any tutorial that i can find further information on this ??
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

On each page view

Delete from `online` WHERE `user` = '$user'

insert into `online` the $user and the current timestamp


Then to figure out whos online
$time = (time()+120);
SELECT distinct(`user`) FROM `online` WHERE `timestamp` > $time;

Basically we are pulling out where the user has made a page view in the last 120 seconds (2 minutes)...... You could use javascript to detect a browser close but no one really bothers doing that, usually 2 minutes of inactivity means they have left.

You can also make a Logout set them as inactive that way if a user logs out they do not see themselves in the online users list :-)
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Post by winsonlee »

does that mean that the delete and insert sql statement is used for everypage within the system or it is used during login only ??

can i have further explanation on how to use javascript to remove the user from the table when the browser is close??
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Yes it goes on every page,
the way I do it is usually:

On every page do:

Code: Select all

<?php
include("timed_actions.php");
?>
Then on "timed_actions.php" put your code to delete from `online` and then `insert` into online.

To use javascript you could make an onunload="function()" in your body tag, then define the function in the head tag, make it put a popup window and make that popup window set them as inactive then close... problem is most people have popup blockers so I would just go with the timed method
Post Reply