If you don't wish to include a file, there are at least three different ways to get a PHP script to execute. I tested with a query string referring to the following file:
hello.php:
<?php
function greet() {
$h = "hello";
$w = "world";
return "$h, $w!";
}
echo greet();
To facilitate the execution of hello.php, one may use any of the techniques depicted in the following code:
<?php
if ( !empty( $_GET['q'] ) ) {
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$file = basename( htmlentities( $_GET['q'] ) ); // "hello.php";
if (file_exists($file)) {
/*$contents = file_get_contents("http://$host$uri/$file"); // works
print_r($contents);*/
/*$output = shell_exec('php -f ./' . $file); // works, too
print_r($output);*/
if (headers_sent()) { // tip fr:https://stackoverflow.com/a/8028987/701302
$url = "http://$host$uri/$file";
die("Please click this link: <a href=\"$url\">$url</a>");
}
else
{
header("Location: http://$host$uri/$file");
exit;
}
}
}
With file_get_contents(), you need to pass in a url or relative url. If you pass in the path to a file, then the function will retrieve the script itself instead of executing it. Also, with file_get_contents() you need to assign any return value to a variable if you wish to capture it.
Another way is to use shell_exec() using commandline PHP with the -f option to parse and execute a PHP file. With this function, too, you must assign any return value to a variable if you wish to access the value.
Lastly, you may instruct PHP to redirect the web server to the specified file using header().
Note: one should not use a $_GET variable without doing some validation. This code checks that there is a value as well as use htmlentities() to avoid a XSS attack.