adamlacombe Posted September 20, 2009 Share Posted September 20, 2009 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! Quote Link to comment Share on other sites More sharing options...
citricsquid Posted September 20, 2009 Share Posted September 20, 2009 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. Quote Link to comment Share on other sites More sharing options...
adamlacombe Posted September 21, 2009 Author Share Posted September 21, 2009 well im having the chat in a popup window with no other links, so when the user clicks log out of chat or closes the window it runs that php. it may not be a great idea but any idea on how to do it? Quote Link to comment Share on other sites More sharing options...
haku Posted September 21, 2009 Share Posted September 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
marynorth21 Posted September 21, 2009 Share Posted September 21, 2009 It's kinda hard to implement with AJAX but it worked on me. 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.