title: register shutdown function is called at the beginning of index.php
body: <p>I am trying to generate a file after a user closes their browser window. When index.php starts running, <strong>register_shutdown_function</strong>('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?<br>
  <strong>index.php</strong></p>
  
  <pre><code>     &lt;?php
        session_start();
        register_shutdown_function('shutdown');
        ignore_user_abort(TRUE);
  
        function shutdown(){
           $handle = fopen('ABORT.txt', 'a');
           fwrite($handle, "file content");
           fclose($handle);
      }
   ?&gt;
  </code></pre>
  
  <p>I wonder if it is something I have to change in php.ini.
  I would appreciate any help.<br>
  UPDATE<br>
  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).</p>
  
  <pre><code>    &lt;?php
        session_start();
        if(connection_aborted())
           shutdown();
        ignore_user_abort(TRUE);
  
        function shutdown(){
           $handle = fopen('ABORT.txt', 'a');
           fwrite($handle, "file content");
           fclose($handle);
      }
   ?&gt;
  </code></pre>