2017-02-12 90 views
-1

PHP我有一个非常特殊的情况。PHP - 获取包含文件中定义的变量

想象我有以下代码:

的index.php

<? 
$a = "1"; 
$b = "2"; 
include("other.php"); 
$c = "3"; 
$d = "4"; 
?> 

other.php

<? 
$x = "11"; 
$y = "12"; 
?> 

然后想象我就不说了有sou文件的rce代码:other.php(这似乎很奇怪,但假设)。然后,我想从源代码index.php中获得一些关于other.php上定义的变量的信息,或者可能是源代码。我的要求不允许我打开文件的内容:“other.php”。

我可以在调用之前和之后存储系统状态:other.php,然后进行状态减法以查看哪些已更改?我不能操纵文件:other.php

[UPDATE]

我的问题,是因为我有一个编码的文件一个网站(在上面的代码,它是:other.php)。该编码通过Zend Guard Loader完成。 Zend在这里执行的操作是将编码后的代码放在下面的代码片段的底部,并在某个时刻将其转换为PHP源代码,然后将其作为源代码执行。我没有原始的源代码,只是编码的代码。

然后我想以某种方式获得该文件的源代码。

这里的问题是,这个代码可以定义函数,具有静态赋值的变量和具有动态赋值的变量(从函数结果获取它的值)。

对我来说理想的是获得源代码的一些方法。

该网站工作正常,所以解码正确完成。

<?php @Zend; 
4123; 
/* This is not a text file */ 
print <<<EOM 
<html><body><a href="http://www.zend.com/products/zend_guard"><img border="0" src="http://www.zend.com/images/store/safeguard_optimizer_img.gif" align="right"></a><center><h1>Zend Optimizer not installed</h1></center><p>This file was encoded by the <a href="http://www.zend.com/products/zend_guard">Zend Guard</a>. In order to run it, please install the <a href="http://www.zend.com/products/zend_optimizer">Zend Optimizer</a> (available without charge), version 3.0.0 or later. </p><h2>Seeing this message instead of the website you expected?</h2>This means that this webserver is not configured correctly. In order to view this website properly, please contact the website's system administrator/webmaster with the following message:<br><br><tt>The component "Zend Optimizer" is not installed on the Web Server and therefore cannot service encoded files. Please download and install the Zend Optimizer (available without charge) on the Web Server.</tt><br><br><b>Note</b>: Zend Technologies cannot resolve issues related to this message appearing on websites not belonging to <a href="http://www.zend.com">Zend Technologies</a>. <h2>What is the Zend Optimizer?</h2><p>The Zend Optimizer is one of the most popular PHP plugins for performance-improvement, and has been available without charge, since the early days of PHP 4. It improves performance by scanning PHP's intermediate code and passing it through multiple Optimization Passes to replace inefficient code patterns with more efficient code blocks. The replaced code blocks perform exactly the same operations as the original code, only faster. </p><p>In addition to improving performance, the Zend Optimizer also enables PHP to transparently load files encoded by the Zend Guard. </p><p>The Zend Optimizer is a free product available for download from <a href="http://www.zend.com">Zend Technologies</a>. Zend Technologies also developed the PHP scripting engine, known as the <a href="http://www.zend.com/products/zend_engine">Zend Engine</a>.</p></body></html> 
EOM; 
exit(); 
__halt_compiler(); 

2003120702‚–ÛUÕ_Eq7X-‡äÂK.½Iëoôïîuolÿ@f*vÈ9õ]¾2003120702‚–ÛUÕ_Eq7X-‡äÂK.½Iëoôïîuolÿ@f*vÈ9õ]¾2003120702‚–ÛUÕ_Eq7X-‡äÂK.½Iëoôïîuolÿ@f*vÈ9õ]¾2003120702‚–ÛUÕ_Eq7X-‡äÂK.½Iëoôïîuolÿ@f*vÈ9õ]¾ 
... the code continues ... 
+0

[get_defined_vars()](http://de2.php.net/manual/en/function.get-defined-vars.php)之前和在索引你'include'后.php,[array_diff()](http://www.php.net/array_diff)和瞧。 – ccKep

+0

让我们假设other.php包含'exec('soemthing bad');'不运行php文件,如果你不知道它们是什么 – nogad

+1

所以你基本上想偷,文件编码的原因 – nogad

回答

0

这可能帮助(注意,我只是说你直接包含的内容,因此代码运行的一些结果...有仍然只是你包括在这种情况下):

<?php 

$a = 1; 
$b = 2; 

$preVars = null; // Define it so it doesn't show up later 
$preVars = array_keys(get_defined_vars()); 

// Normally included, just here for tests sake 
$x = 10; 
$y = 11; 
// End of your include 

$postVars = array_keys(get_defined_vars()); 

$c = 3; 
$d = 4; 

$diff = array_diff($postVars, $preVars); 

echo "New Variables:\n"; 
foreach($diff as $d) 
echo "- \$".$d."\n"; 

输出:

New Variables: 
- $x 
- $y 
+0

谢谢ccKep,我做了一个更新我的帖子关于我的真实原因。你的答案与我所需要的非常接近,但我认为它不适用于包含文件中的函数结果变量赋值。 – Angel

+0

只要变量在include之后可用(即它们在全局范围内),这应该可以正常工作 - 值来自哪里并不重要。 – ccKep