PHP test for fatal error

It's a little long shot, but I decided that I would ask anyway. I have an application that edits web code as you find on Github using the ACE editor. The problem is that you can edit the code that is inside the application itself.

I was able to detect parsing errors before saving the file, which works fine, but if the user creates a runtime error like MyClass extends NonExistentClass , the file passes the parsing check but saves on the file system, killing the application.

Do I need to check at all whether the new code will lead to an error at runtime before I save it to the file system? It seems completely counter-intuitive, but I decided that I would ask.

+7
source share
3 answers

Perhaps to create a JSON object containing fatal error information, use register_shutdown_function . Then use the AJAX call to verify the file; parse the return value from the call to see if there is an error. (Obviously, you can also run the PHP file and parse the JSON object without using AJAX, just by thinking about what would be better from a UX perspective)

 function my_shutdown() { $error = error_get_last(); if( $error['type'] == 1 ) { echo json_encode($error); } } register_shutdown_function('my_shutdown'); 

Will output something like

 {"type":1,"message":"Fatal error message","line":1} 

Prepare this for the start of the test file, then:

 $.post('/test.php', function(data) { var json = $.parseJSON(data); if( json.type == 1 ) { // Don't allow test file to save? } }); 
+2
source

Perhaps useful: php -f <file> will return a non-zero exit code if there is a runtime error.

+2
source

is it possible to first run the code in a separate file and attach some fixed code at the bottom to check if it evaluates?

0
source

All Articles