php 如何停止PHP代码执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21457734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop PHP code execution?
提问by ilyes kooli
Is there a way to immediatelystop PHP code execution?
有没有办法立即停止 PHP 代码执行?
I am aware of exitbut it clearly states:
我知道退出,但它明确指出:
Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.
终止脚本的执行。即使调用 exit ,关闭函数和对象析构函数也将始终执行。
So what I want to achieve is to stop the PHP code execution exactlywhen I call exitor whatever.
所以我想要实现的是在我调用exit或其他任何东西时准确地停止 PHP 代码执行。
Any help?
有什么帮助吗?
Edit: After Jenson's answer
编辑:在简森的回答之后
Trial 1:
试验 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 inwhich was pretty expected.
显示Fatal error: __HALT_COMPILER() can only be used from the outermost scope in这是非常预期的。
Trial 2:
试验 2:
function newExit() {
include 'e.php';
}
echo "start";
newExit();
echo "you should not see this";
e.php just contains __halt_compiler();
e.php 只包含 __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 disablethis logging for some specific conditions based on (remote IP)
我想要做的是基于(远程IP)为某些特定条件禁用此日志记录
回答by The Humble Rat
Please see the following information from user Pekka ?
请参阅来自用户 Pekka 的以下信息?
According to the manual, destructors are executed even if the script gets terminated using die()or exit():
根据手册,即使脚本使用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.
根据这个 PHP: destructor vs register_shutdown_functionPHP:析构函数 vs register_shutdown_function,析构函数not达到 PHP 的执行时间限制时不会执行(在 Apache 2 上确认,在 Windows 7 上为 PHP 5.2)。The destructor also does notget executed when the script terminates because the memory limit was reached. (Just tested)
析构函数也并没有得到当脚本终止,因为内存的限制达到执行。(刚刚测试)
The destructor doesget executed on fatal errors (Just tested) Update: The OP can't confirm this - there seem to be fatal errors where things are different
析构函数确实在致命错误上被执行(刚刚测试过)更新:OP 无法确认这一点 - 似乎存在不同的致命错误
It does notget 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.
如果服务器进程崩溃或发生 PHP 无法控制的其他异常,析构函数肯定不会执行。
Referenced in this questionAre there any instances when the destructor in PHP is NOT called?
在这个问题中引用是否有没有调用 PHP 中的析构函数的情况?
回答by Jenson M John
You can use __halt_compilerfunction which will Halt the compiler execution
您可以使用__halt_compiler停止编译器执行的函数
回答by laurent
You could try to kill the PHP process:
您可以尝试杀死 PHP 进程:
exec('kill -9 ' . getmypid());
回答by CONvid19
Apart from the obvious die()and exit(), this also works:
除了明显的die()and exit(),这也有效:
<?php
echo "start";
__halt_compiler();
echo "you should not see this";
?>
回答by Arrok
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.
这样做很正常,它必须清除您之前调用的所有变量和函数的内存。不这样做将意味着您的内存会一直卡在您的 RAM 中并被占用,如果这种情况发生多次,您将需要重新启动并刷新您的 RAM 以保留内存。
回答by user5332
or try
或尝试
trigger_error('Die', E_ERROR);

