2017-02-12 140 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