4

I have looked at a few topics (here & google) regarding detecting browser exit in php and im not really any clearer on how to do so.

I tried the register_shutdown_function in PHP but that was executing even when i refreshed the browser page.

Can anyone help?

Thanks in advance

Lion
  • 18,729
  • 22
  • 80
  • 110
Woz
  • 475
  • 2
  • 7
  • 14
  • I see, I tried using JS/jQuery but there seemed to be cross browser issues. What I wanted to do is update a database field when the user exits or shuts down the browser. – Woz Jun 01 '12 at 11:49

5 Answers5

9

PHP is a server side language. Browser events are handled by DOM events. On "onunload" or "onbeforeunload" events, sending an AJAX call to a PHP file.

And in this other question there is a flavored explanation of what I'm saying.

Community
  • 1
  • 1
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
4

I don't think there is any foolproof way to detect a browser close button in PHP or Javascript.

It is much safer and better to handle it via timer based polling OR just simple session timeouts on server side.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Yes thats what im finding! – Woz Jun 01 '12 at 11:51
  • @user1402089: Yes that's right, especially if you're trying to update a database field then I would suggest not to rely on any of the available work-arounds as it just won't work for many cases. – anubhava Jun 01 '12 at 11:56
4

Please explain what you want to do when the browser closes, to see if there perhaps is another way to do so.

A web server sends its response to the browser, and then (usually) closes the connection. You'd have to do something in Javascript, but that won't catch all conditions. You certainly can't detect it serverside.

It can be detected using the Javascript onbeforeunload or onunload functions, but that is absolutely not accurately, since it won't detect:

  • a browser crash
  • a browser exit
  • a computer shutdown
  • a link click on the page
  • when going Back in the browser

Also see this answer.

So for example when you want to log out users when they close the browser, you'd better use a "keepalive" mechanism instead of a "say goodbye" one. You can then log those users off on the server (e.g. using cron) whose sessions have not been active (i.e. who haven't sent a "keepalive") for more than X minutes.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
3

One solution that is at least fool resistant is to send a heartbeat to the server using Javascript and Ajax, then assuming that the browser window has been closed when the signal stops pulsing.

Another would be to use web sockets to maintain a constant connection until the browser window closes.

In any case it would take quite a bit of work from your part to set it up

Hubro
  • 56,214
  • 69
  • 228
  • 381
3

Not just with PHP.

PHP runs server-side, and is far done processing your page by the time the user will have a chance to close their browser. You could technically detect if PHP was still processing the page and the user closes it, with a specific configuration. However, it is not ideal. See connection_aborted().

What you need to do is set up a long-polling connection with JavaScript, and monitor it server-side. You will then get an idea for when that window is closed. That connection could be made to your PHP script, allowing PHP to check connection_aborted(). Note that you will need to set up ignore_user_abort() for this to work, or configure PHP.ini accordingly.

Brad
  • 159,648
  • 54
  • 349
  • 530