Search code examples
phpxmlhttprequest

How can I execute remaining php code after calling "exit"?


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.


Solution

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