Jump to content

How do I add when loading the page and delete when leaving the page


TeddyKiller

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.