【问题标题】:Please help me to solve PHP code [closed]请帮我解决PHP代码[关闭]
【发布时间】:2016-04-21 06:08:24
【问题描述】:

在我的代码中有 3 个部分。顶部和底部必须执行。如果输入代码错误,中间部分必须隐藏。

这是我的代码:

 //correct code section. Must execute     
 echo "must show top";        
 //may contain wrong code. But this portion should not affect on lower
 portion code     
 ob_start();     
 // sample wrong code     
 function jk($i){};     
 function jk($i){ echo $i; }';     
 ob_end_clean();   
 //correct code section. Must execute     
 echo "must show bottom";

当前输出:

致命错误:无法重新声明 jk()(以前......

预期输出:

必须显示顶部必须显示底部或必须显示顶部

致命错误:无法重新声明 jk()(以前......

必须显示底部

【问题讨论】:

  • 为什么要重新声明一个已经创建好的函数?
  • 这是你的作业吗?
  • 制作一个虚拟的错误代码。此部分供用户使用。
  • 有什么办法可以隐藏致命错误吗?
  • 您是否尝试实现类似sandbox.onlinephpfunctions.com

标签: php


【解决方案1】:

您不能以 这种 方式执行此操作,因为 PHP 文件在 其任何部分 执行之前已被编译(到字节码,但无论如何)。 p>

可以通过将中间部分包裹在try/catch中来处理小错误,但致命错误仍然会使整个脚本无法运行。

所以你可以做的就是把中间代码放在别的地方,然后从整个过程范围外部访问它。如果这两个部分需要交换信息(例如一些变量),这可能会很棘手。

最简单的方法是将代码保存到一个临时文件中,然后使用 URL get_file_contents 从 PHP 中访问它:

// Construct PHP tag -- hiding it from editor :-)
$prolog = '<'; $prolog .= '?'; $prolog .= "php\n";

// prolog code contains PHP code, but here we treat is as text,
// so all $'s must be escaped. Same goes for epilog.

$prolog .= <<<PROLOG

// This is common code to make what follows work.
// We can pass any data to this script by creating a *second* file
// and passing the name of this second file via GET to avoid POST
// size limits.
// To avoid arbitrary inclusion (just in case), we could check that
// the file name thus passes conforms to a given syntax, e.g. 32
// random hex characters, no URLs or /etc/passwd or funny stuff.
if (isset(\$_GET['envfile'])) {
    \$name = basename(\$_GET['envfile']) . '.txt';
    \$data = unserialize(file_get_contents(\$name));
    unlink(\$name);
} else {
    \$data = null;
}

PROLOG;

$epilog = <<<EPILOG
    // The code is now hopefully loaded and defines a main()
    // function.
    if (!function_exists('main')) {
        die("-ERR: no main function");
    }
    try {
        die(json_encode(main(\$data)));
    } catch (\Exception \$err) {
        die("-ERR: main failed: {\$err->getMessage()}");
    }
EPILOG;

$unique   = uniqid();
// "special dir" is NOT available from THIS site, and is the webroot of another "sandbox" web site
$physical = "./special-dir/{$unique}.php";
// file must contain a full PHP script with tags and all
file_put_contents($physical, $prolog . $code . $epilog);

// Now ask the file to the web server with its "sandbox" personality
$response = file_get_contents("http://sandbox/{$unique}.php");

上面使用了一个虚拟主机,它应该只能从本地主机本身访问,(除了小心的安全性:禁用敏感功能,在它自己的 webroot 之外无法访问,...)应该打开完整的调试信息和错误报告。

所以如果你得到一个 PHP 错误页面,你就知道发生了一个错误(你甚至可以解析它)。否则,脚本可能会返回,例如您可以导入的 JSON 编码的变量对象:

if (0 === strpos($response, '-ERR')) {
    // $response is a simple error message
}
$package = json_decode($response, true);
if (false === $package) {
    // $response is probably a fatal error HTML page
}
// Do something with $package.

回顾一下:

  • 从用户那里获取代码
  • 将它与适当的序言/结语存根混合到例如初始化一些变量以使它们可用于“子”脚本,并以 JSON 格式返回数据
  • 将生成的代码保存到网站根目录中的临时文件中
  • 具有完整调试功能的安全“沙盒”网站
  • 调用沙盒 URL 以生成第二个 PHP 解释器并查看代码
  • 解释结果

注意:在 PHP 7 中,新的 code parse tree approach 可能允许您直接验证代码,而无需保存并发送到新的 PHP 解释器。然后,如果没问题,您可以直接对其进行 eval(),但请注意这样做会执行当前脚本范围内的代码,它具有更高的权限。您很可能会在很短的时间内让您的沙盒 pwn3d 和/或被滥用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多