在eval上得到致命错误()

时间:2014-02-24 04:04:27

标签: php codeigniter

我正在制作一个将在字符串中执行PHP代码的项目。因为我没有使用任何PHP编译器或解释器。我决定使用eval()函数..我使用Codeigniter Framework

我的代码

CONTROLLER

$code  = $this->input->post('code');
$functionCall = $this->input->post('funCall');
$expOut = $this->input->post('expectedOutput');
$integrate = $code." ".$functionCall;
     ob_start();
     eval($integrate);
     $ret = ob_get_contents();
     ob_end_clean();

if($ret === $expOut){
        //all work fine
   }
else{
    redirect('main/wrongCode');
    }
 }

一切正常但输出是致命错误时..不会执行redirect('main/wrongCode'); .. 是否有办法获得致命错误?所以我能成为一个条件?

1 个答案:

答案 0 :(得分:0)

我认为您可以使用PHP异常处理

try{
     ob_start();
     eval($integrate);
     $ret = ob_get_contents();
     ob_end_clean();
}catch(Exception $e) {
     echo 'Caught exception: ',  $e->getMessage(), "\n";
}
if($ret === $expOut){
     //Do sth
}
else{
    // Do sth else
}

查看PHP Exceptions