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);
}
?>