Jump to content

window close?


adamlacombe

Recommended Posts

I have googled everything possible to find the answer to this.

I need to use javascript to close a window, but when it closes I need it to run some php:

<?php
if($windowcloses){
   $fp = fopen("chat.html", 'a');
   fwrite($fp, "<i>User <a href='index.php?action=profile&username=". $_SESSION['username'] ."'>". $_SESSION['username'] ."</a> has left the chat session.</i><br>");
   fclose($fp);
?>

 

is that possible, and if so what is the code I need to use?

Thanks in advanced!

Link to comment
Share on other sites

It's impossible to be 100% accurate in knowing if a user closes the page. If you're building a chat application you'll be constantly polling the server, correct? So have the server check if the user is still polling, you can assume if after xx seconds the user hasn't polled that they're no longer connected. You shouldn't rely on the client to provide disconnection/connection messages, user the serverside to do that.

Link to comment
Share on other sites

You can do this with AJAX, adding a handler to the logout link that hits a php script on the server, and using onbeforeunload for the close button. Only problem being that onbeforeunload is not fully crossbrowser compatible, and therefor won't work on all browsers.

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.