我希望eval()也使调用它的函数结束。但我不想阻止整个申请。
我使用include将第三方应用程序包含到我自己的应用程序中。第三方应用程序最终调用一个函数,该函数首先允许我通过钩子注入代码,然后在函数的末尾简单地调用exit();。
我不想手动编辑他们的代码。我也不想使用exec()、readfile()或curl()或通过http或任何其他类似方法包括第三方应用程序。因为我希望客户端上下文在第三方脚本中保持存在。(否则,第三方脚本会认为我自己的服务器就是客户机。例如,第三方脚本将始终看到$_SERVER['REMOTE_ADDR']是127.0.0.1。我不想那样。)
简言之,正在发生以下情况:
我的申请:
// do stuff
// ...
chdir("path/to/third/party/application");
include ("path/to/third/party/application/index.php");
chdir("path/to/my/application");
// ...
// do more stuff第三方申请:
// do some stuff
// ...
doLastStuff();
function doLastStuff() {
// doing some last stuff
// ...
$hook = "..."; // get code from database
if ($hook) {
eval($hook);
}
exit();
}问题是,最后的exit();也会停止我自己的脚本。我不想那样。有人能找到一种方法来避免exit()从钩子里面停止我的脚本吗?
我可以将任何字符串放入$hook中。
发布于 2015-04-03 13:02:32
我自己已经修好了。诀窍是在eval'd代码中引发一个异常。并在我自己的应用程序中尝试/捕获包含。
我的申请:
// do stuff
// ...
chdir("path/to/third/party/application");
try {
include ("path/to/third/party/application/index.php");
}
catch (Exception $e) {
}
chdir("path/to/my/application");
// ...
// do more stuff第三方申请:
// do some stuff
// ...
doLastStuff();
function doLastStuff() {
// doing some last stuff
// ...
$hook = "throw new Exception();"; // get code from database
if ($hook) {
eval($hook);
}
exit(); // will no longer be executed...
}我还可以引发一个自定义派生的异常类,因此我只能捕获自己的异常,并保留其他异常。
https://stackoverflow.com/questions/29431819
复制相似问题