h0mayun
h0mayun

Reputation: 3621

continue php script execution called by ajax after

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

Upvotes: 0

Views: 762

Answers (3)

dualed
dualed

Reputation: 10512

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.

Upvotes: 1

Pedro Cordeiro
Pedro Cordeiro

Reputation: 2125

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

Upvotes: 0

Brandon - Free Palestine
Brandon - Free Palestine

Reputation: 16676

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.

Upvotes: 0

Related Questions