nikhilthecool Posted March 29, 2006 Share Posted March 29, 2006 hii want to run a php script after the window is closed.how do i do that?basically, i need to call the script for logout when the user clicks the window close (X) button.pls helpthanks Quote Link to comment https://forums.phpfreaks.com/topic/6097-call-script-on-closing-a-window/ Share on other sites More sharing options...
shocker-z Posted March 29, 2006 Share Posted March 29, 2006 This is not possible with PHP as it is all executed when the page loads therefore to process code when a windows is closed needs to be done via JavaScript but then you have problems with some people disabling javascript and so on..I wanted to do this a long time ago to create my online stats for an old site but i ended up using datetime in a database and basing it on an action in past 5 minutes as it being an online user then each page they click it updated the datetime of visit..If you are wanting to log a user out then you could always log a cookie with the session ID in and then on each page look to see if the user has the same session if not then they have oviusly exited all the pages on your site and tryed accessing it again..Maby a litle more info on exacly what you are trying to acheive :)RegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/6097-call-script-on-closing-a-window/#findComment-21953 Share on other sites More sharing options...
kenrbnsn Posted March 29, 2006 Share Posted March 29, 2006 With the AJAX functions in Javascript, it is possible to invoke a PHP script from Javascript and you should be able to do what you want to do.Ken Quote Link to comment https://forums.phpfreaks.com/topic/6097-call-script-on-closing-a-window/#findComment-21964 Share on other sites More sharing options...
ober Posted March 29, 2006 Share Posted March 29, 2006 Right, but you guys are missing the point. Javascript cannot detect when a user closes the browser window. You're going to have to use shocker's method of checking update times. Quote Link to comment https://forums.phpfreaks.com/topic/6097-call-script-on-closing-a-window/#findComment-21968 Share on other sites More sharing options...
nikhilthecool Posted March 30, 2006 Author Share Posted March 30, 2006 ok. thanks guys.i'll try to implement shocker's datetime logic probably.Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/6097-call-script-on-closing-a-window/#findComment-22198 Share on other sites More sharing options...
Guest footballkid4 Posted March 30, 2006 Share Posted March 30, 2006 [!--quoteo(post=359681:date=Mar 29 2006, 07:46 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 29 2006, 07:46 AM) [snapback]359681[/snapback][/div][div class=\'quotemain\'][!--quotec--]Right, but you guys are missing the point. Javascript cannot detect when a user closes the browser window. You're going to have to use shocker's method of checking update times.[/quote]Yes, it can...[code]<body onUnLoad="function_here()">[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6097-call-script-on-closing-a-window/#findComment-22202 Share on other sites More sharing options...
nikhilthecool Posted March 30, 2006 Author Share Posted March 30, 2006 i already tried onUnload()But the prob here is that, after i close the window, i.e., when onUnload is called, i have to run a script for LOGOUT which works fine on closing the window.But, this also works when i REFRESH the page. So whenever the user refreshes the page, he gets logged out and relogged in automatically. lol.i guess onUnload isnt appropriate. But is there any logic wherein in can prevent the user from refreshing a page?? Quote Link to comment https://forums.phpfreaks.com/topic/6097-call-script-on-closing-a-window/#findComment-22209 Share on other sites More sharing options...
ober Posted March 30, 2006 Share Posted March 30, 2006 No, you cannot stop a user from refreshing the page through the browser. And AFAIK, closing the actual browser window does not fire the onUnload() function. Leaving the page will, however, but that doesn't fix your problem. Quote Link to comment https://forums.phpfreaks.com/topic/6097-call-script-on-closing-a-window/#findComment-22284 Share on other sites More sharing options...
Guest footballkid4 Posted March 30, 2006 Share Posted March 30, 2006 [!--quoteo(post=360004:date=Mar 30 2006, 05:31 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 30 2006, 05:31 AM) [snapback]360004[/snapback][/div][div class=\'quotemain\'][!--quotec--]No, you cannot stop a user from refreshing the page through the browser. And AFAIK, closing the actual browser window does not fire the onUnload() function. Leaving the page will, however, but that doesn't fix your problem.[/quote]It does get fired when the user closes the window, I've had to use it in my scripts before. I did forget about the refershing though.I say go with shocker's idea, the time frame would be best. Quote Link to comment https://forums.phpfreaks.com/topic/6097-call-script-on-closing-a-window/#findComment-22292 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.