Created
January 28, 2022 01:35
-
-
Save kugland/8841e0819a2f1f63a2f45d83a1cd453f to your computer and use it in GitHub Desktop.
Get all variables from a PHP script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// | |
// Get all variables from a PHP script. | |
// | |
// Warning: This is probably not a good idea security-wise. | |
// | |
// Usage: | |
// | |
// Let's say you have a script called 'test.php', which contains the following: | |
// | |
// <?php | |
// $my_var = "Hello, my variable!"; | |
// define('MY_CONST', 'Hello, my constant!'); | |
// echo 'Can you see me?'; | |
// | |
// Then you can call this function from another script, like this: | |
// | |
// $vars = get_script_vars("test.php"); | |
// var_dump($vars); | |
// | |
// You'll get this as output: | |
// | |
// array(2) { | |
// ["my_var"]=> | |
// string(19) "Hello, my variable!" | |
// ["MY_CONST"]=> | |
// string(19) "Hello, my constant!" | |
// } | |
// | |
if (isset($argv[1]) && $argv[1] == '__get_script_vars_inner__') { | |
ob_start(); // Let's send the output to... | |
include $argv[2]; | |
ob_end_clean(); // ... /dev/null | |
$vars = get_defined_vars(); | |
$consts = get_defined_constants(true); | |
$vars = array_merge($vars, $consts['user']); | |
$verboten = ['argv', 'argc', '_SERVER', '_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_REQUEST', '_ENV']; | |
foreach ($verboten as $var) { | |
unset($vars[$var]); | |
} | |
echo json_encode($vars); | |
} else { | |
function get_script_vars($file) { | |
$oldlocale = setlocale(LC_CTYPE, 0); | |
setlocale(LC_CTYPE, "en_US.UTF-8"); | |
$my_path = escapeshellarg(__FILE__); | |
$script_path = escapeshellarg($file); | |
setlocale(LC_CTYPE, $oldlocale); | |
$pid = posix_getpid(); | |
$php = escapeshellarg(exec("/bin/readlink -f /proc/$pid/exe")); | |
$output = shell_exec("$php $my_path __get_script_vars_inner__ $script_path"); | |
return json_decode($output, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment