Php 是否有任何方法可以使用eval()捕获致命错误?

Php 是否有任何方法可以使用eval()捕获致命错误?,php,Php,你能帮我完成第二个功能吗?事实上,我不确定这是否可能 有一个简单的方法。将PHP代码放在另一个文件中。例如:index.php和check.php。将PHP代码放在check.PHP中 现在在index.php中编写: $code = 'php statement'; // getting perse error function perse_error_check($code){ if(eval($code) === "true"){ return "no perse error";

你能帮我完成第二个功能吗?事实上,我不确定这是否可能

有一个简单的方法。将PHP代码放在另一个文件中。例如:index.php和check.php。将PHP代码放在check.PHP中

现在在index.php中编写:

$code = 'php statement';

// getting perse error
function perse_error_check($code){
 if(eval($code) === "true"){
   return "no perse error"; 
 }

 if(eval($code) === "false"){
  return "perse error found"; 
 }
}


// getting fatal error
function fatal_error_check($code){
.......................................
.......................................
}

下面将PHP代码写入另一个文件。 然后,它使用命令行执行来解析文件并检查错误:

$check_data = file_get_contents("yourhosturl/allotherdirectory/check.php");

if(preg_match("/Fatal error/",$check_data)){
    echo "fatal error found"; 
}
/*将要测试的代码放在单独的文件中:*/
$code='';
文件内容('check.php',$code);
/*解析该文件并存储来自PHP解析器的消息*/
$x=exec('php-l check.php');
/*检查PHP解析器返回的消息*/
if(preg_匹配('/Errors/i',$x))
{
echo“代码有错误!”;
}
其他的
{
echo“代码看起来不错!”;
}
/*删除该文件*/
取消链接('check.php');

好处是代码不会运行,只会被解析。然而,我假设您会将代码写入一个文件并使用它。。。所以就像其他人提到的,要非常小心。使用类似open_basedir(并测试它)的方法来限制对特定目录的访问,在将代码包括在生产中之前手动检查代码。。。等等。

使用eval()的最大问题之一(在安全风险之后)是,您无法使用它进行任何错误处理非常简单,就是不要使用它!切勿使用
evil()
Yikes。听起来像是一场灾难。你想做什么,你认为eval是解决方案?很可能有更好(而且肯定更安全)的方法。
$code
在传递给
eval()
时,如果它是单引号,则根本不会展开求值。如果
$code
有任何恶意内容,您现在可以使用此功能完全访问文件系统、数据库等。
eval
和执行代码字符串的真正危险之一是。
$check\u data
将PHP代码作为字符串,它不会对其求值或解析。看我对这个问题的回答。
/* Put the code to be tested in a separate file: */
$code = '<?php echo "Test"; ?>';
file_put_contents('check.php', $code);

/* Parse that file and store the message from the PHP parser */
$x = exec( 'php -l check.php');

/* Check the message returned from the PHP parser */
if(preg_match('/Errors parsing/i',$x))
{
    echo 'The code has errors!';
}
else
{
    echo 'The code looks good!';
}

/* delete the file */
unlink('check.php');