0

i have a page called call.php with this codes

    <script src="http://localhost/js/jquery.js"></script>   
    <script>
        $(document).ready(function()
        {
            $.post("exe.php",
            { 
                a: 'a'
            },
            function(data)
            {
                alert(data);
            });
        });
    </script>   
///close tabs after onload

and in exe.php i have this code

<?php

    set_time_limit(60);
    ignore_user_abort(true);
    sleep(20);

    $file = fopen("test.txt","w");
?>

i need to continue php execution even after closing tab immediately it works fine in localhost using WAMP but in my host doesn't work it stops after perhaps 10-14 secs in localhost and server ignore_user_abort(true) is off the question is : turning this function ON solves the problem? and why in localhost it works even when this function is off

my execution takes about 30 sec times i need to get it working

EDIT

i tried something like this

<?php
    ignore_user_abort(true);
function shutdown()
{
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/2.txt',"w");
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/4.txt',"w");
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/6.txt',"w");
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/8.txt',"w");
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/10.txt',"w");
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/12.txt',"w");
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/14.txt',"w");
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/16.txt',"w");
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/18.txt',"w");
    sleep(2);
    $file = fopen($_SERVER["DOCUMENT_ROOT"].'/logs/20.txt',"w");
}
//register_shutdown_function('shutdown');
shutdown();
?>

opened tab and closed immediately after if completely loaded this code created 2.txt ... 10.txt so certainly there is an option which shuts down script after 10 secs

h0mayun
  • 3,466
  • 31
  • 40
  • I thinks this is relevant http://stackoverflow.com/questions/3833013/continue-php-execution-after-sending-http-response – marines Mar 05 '13 at 13:13

3 Answers3

1

On Windows, PHP will buffer the output to a certain amount of Bytes (last time I tested, I believe it was 2048). As long as PHP does not try to flush that buffer, it will not detect a closed connection, and so it will not abort the script. On Linux that buffer works differently and most importantly does not ignore flush().

As to why your ignore_user_abort(true) call "does not work" (this kind of sentence is not a good error report and will not likely result in your getting a good answer) I can not possibly know.

  • You could however try to use register_shutdown_function instead.

  • You could also try increasing the max execution time using a .htaccess file. The syntax is quite easy

    php_flag max_execution_time 120
    
  • The web server itself also may have an execution time limit that may abort your script, see TimeOut for example.

dualed
  • 10,262
  • 1
  • 26
  • 29
  • what did you try, how did it not work. And yes. Since `set_time_limit()` and `max_execution_time` are both disabled when in safe mode. Also note that [**safe mode**](http://php.net/manual/en/features.safe-mode.php) is deprecated in PHP 5.3 and removed (throws a fatal error) in PHP 5.4 and above – dualed Mar 05 '13 at 13:33
  • Tried this code opened call.php after 2-3 second closed tab ,after 20 secs file did not created,also i closed tab after 14 secs but still file didnt created,although it works by not closing tab – h0mayun Mar 05 '13 at 13:43
  • I think this has to be a configuration issue, for starters get a host without `safe_mode`, it's not safer anyway. – dualed Mar 05 '13 at 14:03
0

You want to use the ignore_user_abort function.

Definition:

Set whether a client disconnect should abort script execution

Warning: Be careful with this, the only way to kill an infinite loop would be to kill the web server/PHP depending on your setup.

Brandon
  • 16,382
  • 12
  • 55
  • 88
0

You need to call ignore_user_abort and set_time_limit before you call sleep.

Pedro Cordeiro
  • 2,085
  • 1
  • 20
  • 41