Jump to content

Archived

This topic is now archived and is closed to further replies.

nikhilthecool

Call script on closing a window

Recommended Posts

hi

i 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 help

thanks

Share this post


Link to post
Share on other sites
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 :)

Regards
Liam

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest footballkid4
[!--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]

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest footballkid4
[!--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.

Share this post


Link to post
Share on other sites

×

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.