How to stop PHP code execution?

旧巷老猫 提交于 2019-11-26 21:29:06

问题


Is there a way to immediately stop PHP code execution?

I am aware of exit but it clearly states:

Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.

So what I want to achieve is to stop the PHP code execution exactly when I call exit or whatever.

Any help?

Edit: After Jenson's answer

Trial 1:

function newExit() {
    __halt_compiler();
}    
echo "start";
newExit();    
echo "you should not see this";

Shows Fatal error: __HALT_COMPILER() can only be used from the outermost scope in which was pretty expected.

Trial 2:

function newExit() {
    include 'e.php';
}
echo "start";
newExit();
echo "you should not see this";

e.php just contains __halt_compiler();

This shows startyou should not see this

Edit: Why I want to do this?

I am working on an application that includes a proprietary library (required through virtual host config file to which I don't have access) that comes as encrypted code. Is a sort of monitoring library for security purpose. One of it's behaviours is that it registers some shutdown functions that log the instance status (it saves stats to a database)

What I want to do is to disable this logging for some specific conditions based on (remote IP)


回答1:


Please see the following information from user Pekka 웃

According to the manual, destructors are executed even if the script gets terminated using die() or exit():

The destructor will be called even if script execution is stopped using exit(). Calling exit() in a destructor will prevent the remaining shutdown routines from executing.
According to this PHP: destructor vs register_shutdown_function, the destructor does not get executed when PHP's execution time limit is reached (Confirmed on Apache 2, PHP 5.2 on Windows 7).

The destructor also does not get executed when the script terminates because the memory limit was reached. (Just tested)

The destructor does get executed on fatal errors (Just tested) Update: The OP can't confirm this - there seem to be fatal errors where things are different

It does not get executed on parse errors (because the whole script won't be interpreted)

The destructor will certainly not be executed if the server process crashes or some other exception out of PHP's control occurs.

Referenced in this question Are there any instances when the destructor in PHP is NOT called?




回答2:


You can use __halt_compiler function which will Halt the compiler execution

http://www.php.net/manual/en/function.halt-compiler.php




回答3:


You could try to kill the PHP process:

exec('kill -9 ' . getmypid());



回答4:


Apart from the obvious die() and exit(), this also works:

<?php
echo "start";
__halt_compiler();
echo "you should not see this";
?>



回答5:


I'm not sure you understand what "exit" states

Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.

It's normal to do that, it must clear it's memmory of all the variables and functions you called before. Not doing this would mean your memmory would remain stuck and ocuppied in your RAM, and if this would happen several times you would need to reboot and flush your RAM in order to have any left.




回答6:


or try

trigger_error('Die', E_ERROR);



回答7:


this can be possible through, The exit() function prints a message and exits the current script. This function is an alias of the die() function.

enter link description here



来源:https://stackoverflow.com/questions/21457734/how-to-stop-php-code-execution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!