1

I am trying to generate a file after a user closes their browser window. When index.php starts running, register_shutdown_function('shutdown') is called and the file is generated as if it was shutdown() alone. But once the window is closed, the function shutdown() will not be called. Why is shutdown() being called at the beginning? Why will it not be called on shutdown?
index.php

     <?php
      session_start();
      register_shutdown_function('shutdown');
      ignore_user_abort(TRUE);

      function shutdown(){
         $handle = fopen('ABORT.txt', 'a');
         fwrite($handle, "file content");
         fclose($handle);
    }
 ?>

I wonder if it is something I have to change in php.ini. I would appreciate any help.
UPDATE
Can I use connection_aborted() to know when the user disconnects? As you can see I am new to PHP. I need to delete some files created by the user once they get disconnected and they don't logout properly(e.g.Power goes out).

    <?php
      session_start();
      if(connection_aborted())
         shutdown();
      ignore_user_abort(TRUE);

      function shutdown(){
         $handle = fopen('ABORT.txt', 'a');
         fwrite($handle, "file content");
         fclose($handle);
    }
 ?>
Moto Raton
  • 47
  • 6
  • 2
    `register_shutdown_function` has nothing to do with closing window. It is called when script ends or fails ( with some exception ). php can't know if a window is closed. you have to use js/jQuery for that. – fusion3k Mar 22 '16 at 01:18
  • Thanks for your answer. I tried using js, but onunload and onbeforeunload won't work for Mozilla. Any ideas? – Moto Raton Mar 22 '16 at 01:44

1 Answers1

1

You need to subscribe to the unload event, or possibly the beforeunload event : https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload

What you will need is an AJAX endpoint written in PHP on the server to capture the information you need, and javascript on the client side to send to it on unload. Can the unload Event be Used to Reliably fire ajax Request?

You don't disconnect from PHP. HTTP is a stateless protocol, you don't keep connections open. You ask PHP for a page, it gives you the page, and the connection is closed. If you want another page, you open another connection. Every request is independent. So, no, you can't use connection_aborted. The purpose of that is to detect if a connection was broken whilst you were mid-transfer.

If your intention is to detect that the browser was shut mid-transfer, then continue to work afterwards, then you can use ignore_user_abort to prevent the script from being cancelled, and then do your own clean-up server-side. http://php.net/manual/en/features.connection-handling.php is where you need to read.

Community
  • 1
  • 1
Xaraxia
  • 199
  • 9
  • What happens if the user loses power and then the browser can't call onbeforeunload? I need to delete files on the server left by the user that I don't want to be accumulated overtime. – Moto Raton Mar 22 '16 at 14:29
  • 1
    You can add ajax request that do simple get to a server in order to tell server that user is still active. If no request is done inside 1 minute - then user closed window. 1m lag for deleting files usually is not a problem. – Oleg Liski Mar 22 '16 at 14:40
  • If you accept a small amount of lag, there is a PHP-only solution too. If you use PHP's session management, and put the filenames into the session storage, you can go through any sessions that haven't been accessed in a certain period of time and delete any files listed inside them. This will require you to disable automatic session clean up so the sessions don't get deleted before your script runs, so you will then also have to clean up your session files manually (you could do this as part of your file cleanup process). See [PHP Sessions](http://php.net/manual/en/book.session.php) – Xaraxia Mar 23 '16 at 21:09