TeddyKiller Posted March 28, 2010 Share Posted March 28, 2010 It's hard to explain. I have a file called "mypage.php" for example. When you load the page, it should insert your user data into a specific table in the database. When a user leaves this page, to go onto another page, it should remove them from the database. Any ideas how I can do it? Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 To insert the data just write a standard insert query at the beginning of the page. To delete you can do it two ways, one you can place the delete query at the very end of the page so when it finishes loading it has deleted, or you can store a record in the session to indicate that certain things should be deleted from the db, that way when the user goes to the next page on your site you can run the delete query then. However this doesn't work if the user visits another site or closes the browser before delete can kick in on the new page, for obvious reasons. The only other alternative is quite complex and uses a mix of ajax and cron. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 28, 2010 Author Share Posted March 28, 2010 Hmmmm.. yeah, well maybe it was a page which was a popup. (Brand new window.. with no toolbars or anything) How would that work? If someone clicked the X (only way to exit the chat in this case) to delete from the query? Is it easier that way Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 Just closing the window wouldn't delete the data from the db. What are you trying to do, maybe there's an easier way? Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 28, 2010 Author Share Posted March 28, 2010 I have a little chat script, at the moment it opens in the same page, but will be changed into another one. I'm not sure this is the best way... I'll create a database table, called chatusers When they open the chat, it'll insert there username etc, and show up in the "users chatting" table thing nex to the chat (I haven't done this yet) When they close the chat.. it should remove them from the database, so that they are no longer chatting... If that makes sense? The chat is done with AJAX. Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 Yeh it does, ok here is what you should do. Create a new column in the user table called last_active or something similar. Now since you are already using ajax you should extend what you currently have to update the last_active column every 10 seconds of so with the current timestamp. Now when displaying whether the user is online or not simply take the current time minus the last_active timestamp and if the difference is say greater than 20 seconds the user is no longer online. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 28, 2010 Author Share Posted March 28, 2010 Though what if your there.. but not actually off the page, so you're still on the chat.. just not using it? If you get me. Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 If you want to pick up on users being idle then it gets a little complicated, you could retain the automatic query to update the users timestamp put also have another column for the time of the last message sent, then use a wider margin of say a few minutes and the users status could still be left as in chat (according to the 10 second update) but also idle (based on when the last message was sent), does that help? Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 28, 2010 Author Share Posted March 28, 2010 Hmmm.. You see I'm not an expect. However.. what could be possible, is a logout button, that should do the trick wouldn't it, and enable it to close that window. Then the idea would be to remove the X at the top of the screen .. Is that possible? Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 No you can't remove the window controls at the top unfortunately, and when was the last time you actually used a logout button over the window controls, I bet pretty rarely right? Honestly your best bet is to work of update times, you will this way always be able to tell when a user has the chat window open and if they are idle or not. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 28, 2010 Author Share Posted March 28, 2010 Hmmm.. so if you had the window open but weren't talking.. and you got signed out, how would you then get signed back in maybe? Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 Unless you have PHP set to expire sessions after a certain amount of time the user should never get signed out unless the window is closed. If you did get signed out the ajax chat should kick in to display a message with a link prompting the user to refresh the page. Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 When I said "should" I mean you should develop it to do that. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 28, 2010 Author Share Posted March 28, 2010 Unless you have PHP set to expire sessions after a certain amount of time the user should never get signed out unless the window is closed. I don't understand? I know when a browser is closed.. a session is ended, correct? What if a user had multiple windows open, and they closed the chat window. Would this work or not? Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 Correct, if the user keeps their other windows open and closes the chat window then the ajax is no longer updating the database, therefore the user gets labelled as being out of the chat. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 28, 2010 Author Share Posted March 28, 2010 A chat session gets set on there computer, so what I can do.. is set it so that the value is the users ID. Else if it isn't the users ID (eg: the chat session has ended) it'll remove them from the database. That would work, right? If I make sense. Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 The problem you have though is getting the script to execute once the user has left the chat in order to delete the data from the db. Really the only way to be able to maintain accurate info on who is in chat and who is not is to use the ajax example I gave you. The problem with your last idea is that once the user has left no script has been executed in order to bring the db up to date. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 28, 2010 Author Share Posted March 28, 2010 I just had a thought, that wouldn't actually quite work. Well the chat gets refreshed, if I put a query in.. to update there "time active" by there session ID, it'll show there current time active (All users should have the same time basically) When the window is closed, the javascript is no longer running, as you said, there for if I put in another query, for other users .. to check if the any users time active is 10 seconds smaller than the current time stamp, to remove them. However.. what if there is nobody in the chat, the last person to leave.. would still be there until someone else enters? Right? Quote Link to comment Share on other sites More sharing options...
GetPutDelete Posted March 28, 2010 Share Posted March 28, 2010 No because all you need to do is have a cleanup script run each time a user loads the page. So the last user leaves and the chat is now empty, now when the next user comes on the script determines that he too hasn't been active for over 10 seconds and so removes him from the db. This all should happen prior to the page being displayed so that once the house keeping is complete the new user gets an accurate view of what's going on in the chat. Anyway that's enough PHPing for me tonight, good luck with your project. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted March 28, 2010 Author Share Posted March 28, 2010 No, I mean.. if a user is active, it updates the "active" timestamp field in the database, but when the user isn't on the chat, it doesn't update his field, there for the script that other people will run, check if his last actve timestamp, is 10 seconds behind the current timestamp, there for he's been inactive and not on the chat for 10 seconds minimum, so it removes him. Anyway, the best idea is to test it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.