Page 1 of 1
"Who's online" code
Posted: Wed Apr 23, 2003 8:36 am
by oQEDo
I am at present writing a portal site and would like to be able to show users who are currently online (similar to the Users browsing this forum which you can see on this page)
Is this done using a database or application cookies (are they called application cookies in php?)
I have really no idea where to start here and would appreciate if someone can point me to some example code or further reading.
Thanks
Posted: Wed Apr 23, 2003 9:26 am
by d1223m
create a table like this:
session_id char(32)
touch int
when a user accesses the page check if theyre session is in the table.
If it isnt then add them.
update the table setting the touch field to the current time ( as returned by php's time() command);
to get all the users online ( if you look at this site then u'll see that the amount of users is based on a period of 5 mins ) select all the rows from that table where touch>( time()-(5*60)) ie. all the records that have been touched in the last five mins.
at some point you'll want to delete the old records that are in the table. you can do this from either the function to get the users online or whenever a user accesses the site, or you could even do this from some sort of cron job. either way:
delete from table where touch<( time()-(5*60))
you can either store the username in the table as well to get a list of users.
Posted: Wed Apr 23, 2003 10:16 am
by oQEDo
Wow, thanks for that, I'll have to go and get my head round it now but its a great start.
Cheers
Posted: Wed Apr 23, 2003 10:55 am
by AndrewBacca
cheers, ive been tring to work out how to do that for ages, how do i get the session_id ???
Andrew
Posted: Wed Apr 23, 2003 11:17 am
by m3mn0n
Hotscripts has a nice library of this type of thing.
Posted: Wed Apr 23, 2003 11:21 am
by AndrewBacca
cheers, they look intersting!!
Andrew
Posted: Wed Apr 23, 2003 3:05 pm
by oQEDo
Thanks guys, this is all good stuff
Posted: Wed Apr 23, 2003 3:43 pm
by m3mn0n
Anytime.
