faqts : Computers : Programming : Languages : PHP : Common Problems : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 10 people (60%) answered Yes
Recently 2 of 4 people (50%) answered Yes

Entry

How can I tell if the user has aborted the download?
How can I force a script to execute completely even if the user aborts?

Jul 5th, 1999 23:22
Nathan Wallace, Rasmus Lerdorf


Add connection_status() function.  This returns the raw bitfield which 
indicates whether the script terminated due to a user abort, a timeout
or normally.  Note that if ignore_user_abort is enabled, then both the 
timeout state and the user abort state can be active

    http://www.php.net/manual/function.connection-status.php3

Add connection_timeout() function.  This one can be called in a shutdown
function to tell you if you got there because of a timeout

    http://www.php.net/manual/function.connection-timeout.php3

Add ignore_user_abort() function and .ini/.conf directive of same name

    http://www.php.net/manual/function.ignore-user-abort.php3

Fix connection abort detection code - It should now work reliably with 
Apache.  Also added a user-level connection_aborted() function designed
to let people check whether the user aborted the connection in a
user-level shutdown function.

    http://www.php.net/manual/function.connection-aborted.php3

Here is the test script as used by Rasmus when writing the functions:

<?
    set_time_limit(5);
    ignore_user_abort(0);
    register_shutdown_function("done");
    function done() {
        global $i, $fp;

        fputs($fp,"i reached $i\n");
        if(connection_aborted())
            fputs($fp,"** the connection was aborted **\n");
        fclose($fp);
        echo "all done\n";
    }

    $fp = fopen("/tmp/done","w");
    for($i=0; $i<7; $i++) {
        fputs($fp,"i is $i\n");
        for($j=0;$j<1000;$j++) $a=sqrt($j);
        sleep(1);
        echo "$i
................................................................................<br>";
        flush();
    }
    exit;
?>

This should give you some ideas.