Go Back   Dynamic Drive Forums > General Coding > PHP
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 07-11-2006, 04:01 AM
InNeedofHelp InNeedofHelp is offline
Regular Coders
 
Join Date: Feb 2006
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Default Logout help

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:
Code:
<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.
Reply With Quote
  #2  
Old 07-11-2006, 03:59 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

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.
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Reply With Quote
  #3  
Old 07-11-2006, 04:40 PM
InNeedofHelp InNeedofHelp is offline
Regular Coders
 
Join Date: Feb 2006
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh boy.
I think i've got myself into something way over my head. Could you show me an example Twey, please?

Thanks.
Reply With Quote
  #4  
Old 07-11-2006, 05:16 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

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.
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Reply With Quote
  #5  
Old 07-11-2006, 07:16 PM
InNeedofHelp InNeedofHelp is offline
Regular Coders
 
Join Date: Feb 2006
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Holy cow...how long did it take you to write that?

Oh, and thanks.
Reply With Quote
  #6  
Old 07-11-2006, 08:05 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

I don't know, I just wrote it between other things, like everything I do on this forum.

It's not that big
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Reply With Quote
  #7  
Old 07-16-2006, 04:28 AM
InNeedofHelp InNeedofHelp is offline
Regular Coders
 
Join Date: Feb 2006
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Code:
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.
Reply With Quote
  #8  
Old 07-18-2006, 09:20 PM
InNeedofHelp InNeedofHelp is offline
Regular Coders
 
Join Date: Feb 2006
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anybody have an answer?
Reply With Quote
  #9  
Old 07-18-2006, 11:22 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

Quote:
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.
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Reply With Quote
  #10  
Old 07-19-2006, 04:26 AM
InNeedofHelp InNeedofHelp is offline
Regular Coders
 
Join Date: Feb 2006
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OOOoooohhhhhhhhh.
Its all starting to make sense now .
I think i got this under control now.

Thanks Twey.
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:32 AM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.