0

Im making a project that will execute a PHP code in a string..Since im not using any PHP compiler or interpreter..I decided to use eval() function.. Im using Codeigniter Framework

MY CODE

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');
    }
 }

Everything work fine but when the output is fatal error..it will not execute the redirect('main/wrongCode');.. Is there's a way to get the fatal error? So i can make a condition?

Zurreal
  • 191
  • 1
  • 3
  • 15

1 Answers1

0

I think you could use PHP Exception Handling

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
}

have a look at PHP Exceptions

M Reza Saberi
  • 7,134
  • 9
  • 47
  • 76
  • THANKS..i got it now..it really working..im just make a function exception handler inside the object buffering..:) thanks for your idea..:) – Zurreal Feb 24 '14 at 05:40
  • This could work in PHP7 if you `catch( ParseError $e )`, an alternative for PHP 5 is using `php -l` as shown in my answer here: http://stackoverflow.com/questions/28141295/is-there-any-way-to-catch-fatal-error-using-eval/36944015#36944015 – Frank Forte Apr 29 '16 at 17:27