如何停止PHP代码执行? [英] How to stop PHP code execution?

查看:157
本文介绍了如何停止PHP代码执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以立即停止PHP代码执行?

我知道退出,但其中明确指出:

终止脚本的执行.即使调用了exit,关闭函数和对象析构函数也将始终执行.

所以我要实现的是,当我调用 exit 或其他命令时,完全停止PHP代码执行.

有帮助吗?

詹森回答后

审判1:

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

显示Fatal error: __HALT_COMPILER() can only be used from the outermost scope in,这是预期的.

审判2:

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

e.php仅包含__halt_compiler();

这显示开始,您不应该看到此

为什么要这么做?

我正在处理一个应用程序,该应用程序包含一个专有库(通过我没有访问权限的虚拟主机配置文件来要求),该库以加密代码的形式提供.是一种出于安全目的的监视库.其行为之一是它注册了一些关闭功能,这些功能可以记录实例状态(将统计信息保存到数据库中)

我想要做的是禁用该日志记录,以基于(远程IP)的某些特定条件

解决方案

请参见Pekka用户웃

根据手册,即使使用die()exit()终止脚本,也会执行析构函数:

即使使用exit()停止脚本执行,也会调用析构函数.在析构函数中调用exit()将阻止其余的关闭例程执行.
根据此 PHP:析构函数与register_shutdown_function ,析构函数不会在达到PHP的执行时间限制时执行(在Apache 2上确认,在Windows 7上为PHP 5.2).

当脚本由于达到内存限制而终止时,析构函数也不会被执行. (刚刚测试)

析构函数在致命错误上执行(刚刚经过测试)更新:OP无法确认这一点-事情不同的地方似乎存在致命错误

它不会不会在解析错误时执行(因为不会解释整个脚本)

如果服务器进程崩溃或发生PHP无法控制的其他异常,则肯定不会执行析构函数.

此问题参考 在没有任何实例的情况下没有调用PHP吗?

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)

解决方案

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?

这篇关于如何停止PHP代码执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆