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.

8
  • you cann not do it. you have to write the function before exit; or delete the exit; Commented Jul 15, 2012 at 4:56
  • stackoverflow.com/questions/10403521/… Commented Jul 15, 2012 at 4:58
  • 1
    Can't you just call the function before the exit? The main point of calling exit, as the name implies, is to end the current script. Even if there is an workaround, it'd one of those things you shouldn't do imo. Commented Jul 15, 2012 at 4:59
  • See: stackoverflow.com/questions/3833013/… Commented Jul 15, 2012 at 5:00
  • Fabricio Matte - then how to response the client before exit? Commented Jul 15, 2012 at 5:46

5 Answers 5

14

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

Sign up to request clarification or add additional context in comments.

2 Comments

+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.
im sorry. i'd try it.. but register_shutdown_function() is executed before exit and and before response
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

Comments

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.

1 Comment

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.
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.

2 Comments

That works, but it gets a little sticky if you're dealing with thousands of requests.
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.
-5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.