Is there any way to catch fatal error using eval()?

后端 未结 2 659
离开以前
离开以前 2021-01-22 10:41
$code = \'php statement\';

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

 if(eval($code) =         


        
相关标签:
2条回答
  • 2021-01-22 10:58

    There is a simple way. Put your PHP code in an another file. For an example: index.php and check.php . Put your PHP code in check.php.

    Now in index.php write:

    $check_data = file_get_contents("yourhosturl/allotherdirectory/check.php");
    
    if(preg_match("/Fatal error/",$check_data)){
        echo "fatal error found"; 
    }
    
    0 讨论(0)
  • 2021-01-22 11:19

    The following writes the PHP code to another file. It then uses command line execution to parse the file and check for erros:

    /* 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');
    

    The benefit is that the code doesn't run, it just gets parsed. However I assume you would then write that code to a file and use it... so like others mentioned, be VERY careful. Use things like open_basedir (and test it) to restrict access to specific directories, manually check the code before including it in production... etc.

    0 讨论(0)
提交回复
热议问题