Log in

View Full Version : Logout help



InNeedofHelp
07-11-2006, 03:01 AM
Allright so i have a chatroom in which you can click "Logout" and you are then logged out and your name is taken off of the list of people currently in the chatroom. However, if you do not click Logout, and simply close the window, the session variable isn't destroyed and your name is not taken off the list because PHP doesn't do event based functions. At least thats what i think.

So here's my question: Is there a way to, for example, call function Logout() in PHP when the window closes? Or should i use javascript like this:


<script language="javascript">
document.onUnload = document.write('<?php Logout();?>');
</script>


Is it document.onUnload or window.onUnload? I just blanked on that hehe...

Anyway, any help on this would be great.
Thanks.

Twey
07-11-2006, 02:59 PM
That won't work at all, since the PHP is executed before the Javascript, so Logout() will be run every time the page is loaded :) It's window, but as with window.onload, I think document.onunload has the same effect (note the case).

To do this, you must devise a fairly complex system whereby the last-seen time of each user is stored, and set a reasonable timelimit after which any page accesses (by anyone) will log that user out.

InNeedofHelp
07-11-2006, 03:40 PM
Oh boy.
I think i've got myself into something way over my head. Could you show me an example Twey, please?

Thanks.

Twey
07-11-2006, 04:16 PM
http://twey.co.uk/files/chat.phps

It's an actual script I wrote for someone else, so it's a little more complex than it needs to be for an example, but I'm sure you can pick out the relevant bits.

InNeedofHelp
07-11-2006, 06:16 PM
Holy cow...how long did it take you to write that?

Oh, and thanks. :D

Twey
07-11-2006, 07:05 PM
I don't know, I just wrote it between other things, like everything I do on this forum.

It's not that big :)

InNeedofHelp
07-16-2006, 03:28 AM
Allright Twey, I've finally gotten to a point in my process of learning PHP & MySQL that i can read through your chat script that you provided, and find the necessary step to achieve what I'm going for here. I did read it and i now have the basic idea of what i'm doing, but theres one thing i wanted to ask you.


mysql_query("select * from $usertable where logged=1 and DATE_SUB(NOW(),INTERVAL 30 SECOND) > lastseen;");

That query makes pretty much no sense to me, especially the (NOW(),INTERVAL 30 SECOND). I'm pretty sure this is the part where every thirty seconds it checks the user to see if they're still in the chatroom. But would you mind explaining this one query for me? And, what does DATE_SUB do?

Thanks. :D

InNeedofHelp
07-18-2006, 08:20 PM
Anybody have an answer?

Twey
07-18-2006, 10:22 PM
And, what does DATE_SUB do?DATE_SUB() is the key to this one :) It subtracts one date from another. INTERVAL 30 SECOND returns a DATETIME object to the value of 30 seconds.

InNeedofHelp
07-19-2006, 03:26 AM
OOOoooohhhhhhhhh.
Its all starting to make sense now :D .
I think i got this under control now.

Thanks Twey.

InNeedofHelp
07-20-2006, 02:05 AM
Ok, so I've read through your script a couple times, i understand the majority of it. I understand that one line now too. But i'm left with one last question: that part where it checks their last seen agaisnt date_sub(now and interval), but does that mean that the page has to be refreshed every 5 seconds or something? As in the Users Online box would be an iframe where a meta tag refreshes it every several seconds?

InNeedofHelp
07-20-2006, 03:08 AM
One more thing, i see all over your script things like "set lastseen=NOW()".
But nowhere in the script can i find Function NOW() {. or something like that. Is NOW() a built in PHP function that returns the current time in a datetime format or something of that sort?

Twey
07-20-2006, 12:19 PM
that part where it checks their last seen agaisnt date_sub(now and interval), but does that mean that the page has to be refreshed every 5 seconds or something? As in the Users Online box would be an iframe where a meta tag refreshes it every several seconds?No, the message-display box is a frame that refreshes every few seconds, primarily by AJAX but falling back onto meta refreshes if AJAX isn't supported.
One more thing, i see all over your script things like "set lastseen=NOW()".
But nowhere in the script can i find Function NOW() {. or something like that. Is NOW() a built in PHP function that returns the current time in a datetime format or something of that sort?Close, but it's a MySQL function. :)

InNeedofHelp
07-20-2006, 02:59 PM
Ohh thats why its always in the queries. Hehe, i should've been able to pick that out. :p

So if the message box is the one that refreshes, how does the Online Users part check everyones lastseen time? Is that the AJAX part?

Twey
07-20-2006, 05:25 PM
So if the message box is the one that refreshes, how does the Online Users part check everyones lastseen time? Is that the AJAX part?It doesn't. Users viewing the Online Users page aren't considered to be online.

InNeedofHelp
07-20-2006, 06:09 PM
Ohh Ok. So let me just run this over one last time, the user logs in, and then their lastseen variable is set, everytime they send a message, AJAX causes that message page to refresh, thus updating their lastseen time? And thats how that goes?

hehe, sorry for my ignorance. I'm very new to php.

Twey
07-21-2006, 12:06 AM
everytime they send a message, AJAX causes that message page to refresh, thus updating their lastseen time?Not every time they send a message but every time the message box refreshes. If they leave the page the message box stops refreshing, and after the interval they're considered to be logged out (which actually takes place when another user next views the page, although the timestamp is the user's last-seen time).

InNeedofHelp
07-21-2006, 01:34 AM
Aha, so i was pretty much right. That's pretty good for just starting PHP.

Allright one last question, which is pretty easy: INTERVAL 30 SECONDS - could i do something like INTERVAL 500 SECONDS?

Twey
07-21-2006, 04:28 AM
could i do something like INTERVAL 500 SECONDS?Of course. It would have to be substantially more that the refresh period, though.

InNeedofHelp
07-21-2006, 04:16 PM
Ok, so i understand how to do this whole thing in a chatroom now, but i have yet another problem. :o

Say i want to use this in my website, where on every page ther is a little box that says Members Online: 485. (Or something) How would i apply this time based method to each page? Im assuming i would add to every page a function that updates the users LASTSEEN to "NOW()". But then how would i fit in the function that checks their lastseen against the time now? For example the page loads, their lastseen is updated, but if i use the function to check their lastseen there too it will allways check out to be that the user has been seen within the past 30(or so) seconds.

Basically waht i'm asking is, how do Forums such as PHPBB find out how many users are online if they use this same method?

Twey
07-21-2006, 04:43 PM
They use the same method, but with a much longer logout delay -- such as ten or fifteen minutes. If the user hasn't requested another page within that time, they're said to be inactive.

InNeedofHelp
07-21-2006, 06:05 PM
Right but theres no frames that refresh and reset the users lastseen, so on every page, the users lastseen is updated, and then at the same time their time is checked against now. So no matter what they're considered always online. How do you make it so that they're logged off after 15 minutes of not going to a different page?

Sorry if my question makes no sense.

Twey
07-21-2006, 06:29 PM
the users lastseen is updated, and then at the same time their time is checked against now.You have to check their time against now whenever another user accesses a page as well, not just when they do. Basically, every time a page is requested by anyone, member, guest, whatever, you have to go through a list of the users considered online, check their lastseen against now, and update their status appropriately.

InNeedofHelp
07-21-2006, 07:15 PM
OHHHHH!
I get it now hahaha.
Allright well how would i check one user's time when a different user accesses a page? Do i check every single users Lastseen against NOW() everytime a page is accessed?

That would make sense to me. :p

Twey
07-21-2006, 07:32 PM
Do i check every single users Lastseen against NOW() everytime a page is accessed?You only need to check the users that are logged in, and obviously there's no need to check the user who made the request, as you've pointed out above.

InNeedofHelp
07-21-2006, 08:17 PM
Right.
Well, awesome, i finally get it!
Thanks Twey. :D