簡體   English   中英

PHP - 獲取包含文件中定義的變量

[英]PHP - get variables defined on included file

PHP我有一種非常具體的情況。

想象一下,我有以下代碼:

索引.php

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

其他.php

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

然后想象我沒有文件的源代碼: other.php (這看起來很奇怪,但假設)。 然后,我想從index.php的源代碼中獲取有關other.php定義的變量的一些信息,或者可能是源代碼。 我的要求不允許我打開文件的內容:“other.php”。

我可以在調用之前和之后存儲系統的狀態: other.php然后進行狀態減法以查看發生了什么變化?

不幸的是,我無法操作文件: other.php

[更新]

我的問題是因為我有一個帶有編碼文件的網站(上面的代碼是: 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 ...

這可能會有所幫助(請注意,我只是直接添加了包含的內容,因此代碼可以運行並產生一些結果......在這種情況下仍然只有您的包含):

<?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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM