[英]Is it possible to ignore exit of child script from parent script in PHP?
子脚本终止了父脚本,因为它退出了。
由于它是第三方扩展,因此我需要避免任何核心攻击。 是否有可能以某种方式忽略子脚本从父脚本的退出。 我从外部脚本调用其控制器和方法。
parent.php
<?php
require "child.php";
?>
child.php
<?php
does something;
exit;
?>
更新
只要我们不修改子脚本,任何其他解决方案都可以。
如果实际上确实要编辑核心文件,那么可以在“子脚本”内部使用return
。 从PHP文档 :
如果从全局范围调用,则结束当前脚本文件的执行。 如果包含或需要当前脚本文件,则控制权将传递回调用文件。 此外,如果包括了当前脚本文件,则返回的值将作为include调用的值返回。 如果从主脚本文件中调用return,则脚本执行结束。 如果当前脚本文件由
auto_prepend_file
或auto_append_file
配置选项命名,则该脚本文件的执行结束。
但是,如果子脚本具有exit
语句,则无法阻止父脚本阻止子脚本终止该进程。 不幸的是,您无法覆盖此功能。
您可能可以在线程中运行child.php。 使用join等待该线程结束,然后再继续执行主线程。 这样,在child.php中调用exit将终止子线程,而主线程将继续。
class myThread extends Thread {
public function run(){
include "child.php";
//Call methods from child.php here
}
}
$thread = new myThread();
$thread->start();
$thread->join();
非常感谢大家回答这个问题。 最后,我提出了以下替代方案。 我不确定它是否与任何设计模式相似。 尽管child.php已退出,但以下示例不会因循环而终止。
parent.php
<?php
require "temp.php";
for($i=1;$i<10;$i++){
file_get_contents("http://url/temp.php?var=$i");
}
?>
temp.php
<?php
$var = $_GET['var'];
// execute
require "child.php";
$testController = new TestController();
$testController->method($var);
?>
是。 在child.php中,您可以使用return将控件返回给parent.php。 否则,如果不包括child.php,它将继续其正常操作,即退出。
<?php
does something;
return;
exit;
?>
如果要检查是否包含文件或直接运行文件,则可以使用此答案 。 因此,在这种情况下,请检查子文件(如果未包括在内),请转至exit命令,否则继续其余代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.