Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a PHP script abort itself or detect abortion?

PHP scripts can continue executing after the HTTP page request, so how do I finally stop it executing when I'm done with it?

Also, is there an event to detect when the OS is going to forcibly abort the script? or how do I keep an internal timer to predict max_execution_time?

like image 904
Robin Rodricks Avatar asked Sep 15 '09 17:09

Robin Rodricks


2 Answers

exit()/die() will stop a php script.

To know when to stop the script, you'll just have to use microtime as a timer and save as a constant (or fetch from the php.ini file) the maximum execution time.

You can also look at the Connection handling information. Where we have things like connection_aborted() and connection_status()

But what is the problem you're trying to solve?

like image 141
Ólafur Waage Avatar answered Sep 24 '22 01:09

Ólafur Waage


You can use register_shutdown_function to register a function to be called at the end of script execution. You can use this to detect when the script has been terminated and perform a certain set of actions.

like image 39
TJ L Avatar answered Sep 22 '22 01:09

TJ L