PHP Code Execution Lib
Executes Code
This library executes PHP code dynamically with fine-grained control.
Some features:
- Execute PHP code via `eval`.
- Execute PHP code via temporary include files.
- Capture output, return values, and modified variables.
- Inject external variables into executed code.
- Ignore undefined variable errors when needed.
- Safely handle execution failures.
Usage:
Execute PHP Code based in eval:
Executes HTML with PHP code.
include dirname(__DIR__) . "/lib/app.php";
include get_lib("phpscript.PHPScriptHandler");
$contents = 'Hello<?= " My"; ?> World';
$output = PHPScriptHandler::parseContent($contents);
echo "output: $output
";
output: Hello My World
Execute PHP Code with error control:
Executes HTML with PHP code but ignore errors if a PHP variable is not defined.
include dirname(__DIR__) . "/lib/app.php";
include get_lib("phpscript.PHPScriptHandler");
$contents = '<?
$path .= "Foo";
echo "Hello World";
return 234;
?>. Some other html...';
$return_values = array();
$external_vars = array(
"path" => "/tmp/kj2213"
);
$ignore_undefined_vars_error = true; //if true then PHPScriptHandler::parseContent sets the error handler to 'ignore_undefined_var_error_handler' function.
$output = PHPScriptHandler::parseContent($contents, $external_vars, $return_values, $ignore_undefined_vars_error);
echo "output: $output
";
echo "external_vars: " . print_r($external_vars, true) . "
";
echo "return_values: " . print_r($return_values, true) . "
";
function ignore_undefined_var_error_handler($errno, $errstr, $errfile, $errline) {
$is_undefined_var_error = preg_match('/^(Undefined array key|Undefined global variable|Undefined variable|Trying to access array offset on value of type null|Trying to access array offset on null)/', $errstr)
if ($is_undefined_var_error)
return true; //true to ignore error. Don't execute PHP internal error handler, this is, ignore it.
else
return error_handler($errno, $errstr, $errfile, $errline);
}
output: Hello World. Some other html...
external_vars: array(
"path" => "/tmp/kj2213Foo"
)
return_values: array(234)
Execute PHP Code based in include file:
The difference between this method and the parseContent method, is that, if there is an error in some php statement of the $contents, this method returns an empty result, because the error will be catched with no return string. Instead of the parseContent method, that executes each php statement separately returning the rest of the result and only giving error in the catched php statement with error.
include dirname(__DIR__) . "/lib/app.php";
include get_lib("phpscript.PHPScriptHandler");
$contents = '<?
$_GET["bar"] = "Foo";
?>Some html...';
$output = PHPScriptHandler::parseContentWithIncludeFile($contents, $external_vars = array(), $return_values = array(), $ignore_undefined_vars_error = false);
echo "output: $output
";
echo "external_vars: " . print_r($external_vars, true) . "
";
echo "return_values: " . print_r($return_values, true) . "
";
echo "_GET: " . print_r($_GET, true) . "
";
output: Hello World. Some other html...
external_vars: array()
return_values: array()
_GET: array(
"bar" => "Foo"
)