how to Call Function after Page Load completed
I am trying to call a function after the page has been sent to the browser, but I haven't found a way to do so.
I have tried both register_shutdown_function and __destruct but it does not work.
This is the structure I have:
register_shutdown_function('MyClass::Save');
MyClass::Initialize();
CreateAllOutput();
MyClass::Save has a sleep(20) insi开发者_C百科de.
The shutdown function gets called after the CreateAllOutput(), but the truth is that the loading bar on the browser only finishes after the Save function has been executed, waiting 20 seconds... I tried the same using a non static class and __destruct having a sleep(20) inside, and the same happens.
Is there a way of getting the function to only be executed after the output has all been sent to the browser?
Thank you in advance.
Generally: no. There is no possibility except to flush all output and then call it, but the loading bar in the browser won't finish either.
The only thing I can think of is putting an iFrame into the page to call a PHP file with the save function. Or you could make an Ajax call.
A third possibility would be to call exec()
(Documentation). After making a php file executeable you can run it with that method. As mentioned in the documentation:
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
精彩评论