7

How can a client send a request to a server just to trigger an execution, where the server immediately sends a response then executes the requested code? Similar to this:

echo 'the execution is starting';
exit;
execution_function(); // <--- is it possible to this function being executed after exit 

I want the response to be immediate so the client can do other things while the server executes the request.

Mat
  • 202,337
  • 40
  • 393
  • 406
stackunderflow
  • 1,492
  • 1
  • 23
  • 53

5 Answers5

12

You can use register_shutdown_function() to set a callback function which will be executed when PHP exits.

Michael Hampton
  • 9,737
  • 4
  • 55
  • 96
  • 1
    +1 for correct answer. But bear in mind that this will be triggered by anything that causes the program to stop, including a fatal error. This may or may not be what you want, but it's worth being aware of it. – Spudley Jul 15 '12 at 06:08
  • 1
    im sorry. i'd try it.. but register_shutdown_function() is executed before exit and and before response – stackunderflow Jul 15 '12 at 10:43
3

You can use register_shutdown_function() after exit() , die() , even you can register fatal error to your error register log using this function.Quite handy

munna ss
  • 194
  • 1
  • 2
  • 11
1

Thanks to this question I figured out the way to insert and execute code at the end of script. Usefull when you're debugging a website.

function shutdown($var){
    echo '<pre>';
    var_export($var);
    echo '</pre>';
}
// pass get_included_files()
register_shutdown_function('shutdown',get_included_files());
// or something else
register_shutdown_function('shutdown',get_defined_vars());

note: shutdown is executed after script execution finishes or exit() is called.

Hebe
  • 661
  • 1
  • 7
  • 13
  • with ob_start() you can also catch the whole script output generated until the shutdown and log that. It would be useful for logging requests and responses given by your API (it's a standard practice). Also, tools like Sentry doesn't log the whole output. – Marco Marsala Nov 24 '22 at 08:17
0

I don't think that can be done.

Usually what I do is call an exec() to another external script in another PHP file, with the " > /dev/null &" at the end of the command and exit() just after.

So that the script that called the exec() will not be running when the called script is running.

Salocin.TEN
  • 474
  • 1
  • 6
  • 18
  • 1
    That works, but it gets a little sticky if you're dealing with thousands of requests. – Tim Post Jul 15 '12 at 06:01
  • Hi Jerinho, so it does not work? They worked in my case where the PHP are CLI script run via cron. Yeah thousands of requests demands some skill at finding out which ones are running first. – Salocin.TEN Jul 16 '12 at 01:48
-4

It's not possible, and the reason for that is that when you call the exit function, PHP stops processing anything after that. It's normal usage is for your script to stop running if it detects an error for zero possibility of it reaching the success conditional actions.

If you want your script to continue running and doing stuff, place the stuff you want it to do before the exit function.

Pablo Canseco
  • 554
  • 1
  • 10
  • 24