Jump to content


Photo

Call script on closing a window


  • Please log in to reply
8 replies to this topic

#1 nikhilthecool

nikhilthecool
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 29 March 2006 - 01:46 PM

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

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 29 March 2006 - 02:17 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 March 2006 - 03:16 PM

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

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 29 March 2006 - 03:46 PM

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.

Info: PHP Manual


#5 nikhilthecool

nikhilthecool
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 30 March 2006 - 05:27 AM

ok. thanks guys.

i'll try to implement shocker's datetime logic probably.

Thanks again.

#6 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 30 March 2006 - 05:32 AM

[!--quoteo(post=359681:date=Mar 29 2006, 07:46 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 29 2006, 07:46 AM) View Post[/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...
<body onUnLoad="function_here()">


#7 nikhilthecool

nikhilthecool
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 30 March 2006 - 05:55 AM

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??

#8 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 30 March 2006 - 01:31 PM

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.

Info: PHP Manual


#9 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 30 March 2006 - 02:05 PM

[!--quoteo(post=360004:date=Mar 30 2006, 05:31 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 30 2006, 05:31 AM) View Post[/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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users