Php 获取eval()上的致命错误
我正在做一个将以字符串形式执行PHP代码的项目..因为我没有使用任何PHP编译器或解释器..我决定使用eval()函数。。Im使用Codeigniter框架 我的代码 控制器Php 获取eval()上的致命错误,php,codeigniter,Php,Codeigniter,我正在做一个将以字符串形式执行PHP代码的项目..因为我没有使用任何PHP编译器或解释器..我决定使用eval()函数。。Im使用Codeigniter框架 我的代码 控制器 $code = $this->input->post('code'); $functionCall = $this->input->post('funCall'); $expOut = $this->input->post('expectedOutput'); $integrate =
$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');
}
}
一切正常,但当输出是致命错误时..它不会执行重定向('main/errocode')代码>。。
有没有办法得到致命错误?所以我可以提出一个条件 我认为您可以使用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?我用的是5.4.12..xD或jst 5..:)谢谢..我现在得到了..它真的很有效..我只是在对象缓冲区内做了一个函数异常处理程序..:)谢谢你的想法..:)如果你捕获(ParseError$e)
,这可以在PHP7中工作,PHP5的另一种选择是使用PHP-l
,如我的回答所示: