2009-02-18 38 views
0

我與PHP初學者,我接觸到處理HTML表單和我正在學習如何使用它來做更多...PHP函數pt_register

我使用這個函數(和基於谷歌所以很多人),但我真的想了解它在做什麼....

function pt_register() 
{ 
    $num_args = func_num_args(); 
    $vars = array(); 

    if ($num_args >= 2) { 
     $method = strtoupper(func_get_arg(0)); 

     if (($method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) { 
      die('The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV'); 
    } 

     $varname = "HTTP_{$method}_VARS"; 
     global ${$varname}; 

     for ($i = 1; $i < $num_args; $i++) { 
      $parameter = func_get_arg($i); 

      if (isset(${$varname}[$parameter])) { 
       global $$parameter; 
       $$parameter = ${$varname}[$parameter]; 
      } 

     } 

    } else { 
     die('You must specify at least two arguments'); 
    } 

} 

任何人都可以穿行這個用英語給我嗎?

回答

7

它看起來像它試圖成爲一個替代的register_globals

function pt_register() 
{ 
    // Look at the arguments passed in... 
    $num_args = func_num_args(); 
    $vars = array(); 

    // .. we need at least 2 arguments 
    if ($num_args >= 2) { 

     // $method is the middle part of name of one of PHPs old-style variables 
     $method = strtoupper(func_get_arg(0)); 

     if (($method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) { 
      die('The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV'); 
    } 

     // $varname is the whole name of the variable 
     $varname = "HTTP_{$method}_VARS"; 

     // Make the global variable (for example HTTP_SESSION_VARS) accessible from this function 
     // ${$varname} is using a technique called "variable variables" 
     // If $varname == "HTTP_SESSION_VARS" then $$varname (or ${$varname}) is the same as $HTTP_SESSION_VARS 
     global ${$varname}; 

     // For each argument after the method 
     for ($i = 1; $i < $num_args; $i++) { 
      $parameter = func_get_arg($i); 

      // If the parameter exists in the global variable... 
      if (isset(${$varname}[$parameter])) { 
       // .. make it global... 
       global $$parameter; 
       // ... and set its value 
       $$parameter = ${$varname}[$parameter]; 
      } 

     } 

    } else { 
     die('You must specify at least two arguments'); 
    } 

} 

因此,一個例如:pt_register('SESSION', 'foo');呢,實際上

function example() 
{ 
    global $HTTP_SESSION_VARS; 
    global $foo; 
    $foo = $HTTP_SESSION_VARS['foo']; 
} 

恕我直言,這個腳本是過時的和邪惡! 超全局變量$ _SESSION等意味着你不應該這樣做

+0

我只是打字相同,並完全同意你的看法。 – Energiequant 2009-02-18 23:08:41

+0

+1提到它的邪惡。 – strager 2009-02-18 23:09:12

1

它看起來像它的提取存儲在像$ _ POST和數組變量$ _GET,使他們全球化的,而不是訪問變量後與$_POST['var'],就可以只需使用$var。這樣做並不是真的有充分的理由,如果忘記正確清理輸入,它會打開不必要的安全漏洞。例如:

if (/* some condition */) 
    $admin = true; 

... 

if ($admin) 
{ 
    //do powerful stuff here 
} 

這可能會被惡意用戶呼叫的東西的頁面類似

page.php?admin=1 
4

被打破嗷我,
這看起來像一個剪斷代碼代替了已過時register_globals設置。

回到PHP4,所有類型的用戶生成的參數都直接在您的普通變量作用域中創建爲變量。
用簡單的英語,這意味着到index.php?test=helloWorld的調用將導致

<?php 
    echo "$test<br/>"; 
?> 

輸出:

helloWorld<br/> 

這被認爲是一個市長的機會,程序員搬起石頭砸自己的腳。它已被廢除。理由很充分。

考慮下面的代碼:

<?php 

if (isAuthorised($userId, $sessionId)) { 
    $hasAccess = true; 
} 
// some lines 
// of code... 

if ($hasAccess) { 
    echo 'sensitive information'; 
} 

?> 

什麼對子級發生,如果你會調用此方法index.php?hasAccess=1
這是正確的,邪惡的人打電話給你的腳本剛剛獲得了訪問是授權用戶的部分只要。

總之
這是一個模仿的是不再使用(因爲PHP5),因爲它被認爲是一個重大安全風險功能的行爲的功能。

除了使用上面的代碼,你應該訪問用戶提供的參數一樣

<?php 
    echo $_GET['test'].'<br />'; 
?> 

希望這有助於:)

0

我完全不知道爲什麼人會想要使用此功能。它基本上將來自會話,請求或環境的變量複製到全局名稱空間。

您是如何使用它的?如果您要訪問的表單域,使用$ _ POST:

$value = $_POST['value']; 

相反的:

pt_register('POST', 'value'); 
0

這基本上是採取變量進行預定的整個陣列_SESSION,$ _GET,$函數_POST,$ _SERVER,$ _COOKIE和$ _ENV,並將它們移動到具有相同名稱的全局變量中。例如,如果您加載頁面「test.php?value = 5」,您通常會通過$ _GET ['value']訪問該頁面。但是,您可以撥打:

pt_register('GET', 'value'); 

,這將創建設置爲5全局變量名爲$值,它接受來自同一個全局數組任意數量的參數,因此對於一個頁面「test.php的價值=? 5 & another_value = 6 & yet_another_value = 7" 你可以這樣做:

pt_register('GET', 'value', 'another_value', 'yet_another_value'); 

要有$價值$ another_value和$ yet_another_value所有創建並分配給他們在URL中給出的值。如RoBorg所說,它和舊的register_globals做類似的工作,但它可以讓你更多地控制設置哪些變量。

0
function pt_register() 
{ 
    $num_args = func_num_args(); // Get the number of arguments passed to this function 
    $vars = array(); 

    if ($num_args >= 2) { 
     $method = strtoupper(func_get_arg(0)); 
     // if the number of arguments is larger than or equal to 2, get the first one and make it uppercase, assign it to the varible $method 

     if (($method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) { 
      die('The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV'); 
    } 
     // check if $method = GET, POST, SESSION, SERVER, COOKIE, or ENV' if not, die and kill the script 

     $varname = "HTTP_{$method}_VARS"; 
     /* assign the method to the var name 
     * i.e. HTTP_GET_VARS, HTTP_POST_VARS etc 
     */ 

     global ${$varname}; 
     // put the varible into the global varibles 

     for ($i = 1; $i < $num_args; $i++) { // iterate through the arguments 
      $parameter = func_get_arg($i); // assign to parameter 

      if (isset(${$varname}[$parameter])) { 
       // check if a parameter is set in the HTTP var array 
       // i.e HTTP_GET_VARS[$parameter] 
       global $$parameter; 
       //assign the varible name of parameter to the varible name 
       // say parameter was 'content', create a varible named 'content' and make it global 
       $$parameter = ${$varname}[$parameter]; 
       // update the parameter to match its value in the HTTP vars array 
       // i.e $content = HTTP_GET_VARS['content']; 
      } 

     } 


    } else { 
     die('You must specify at least two arguments'); 
    } 

} 
0

太多有用的回答評論他們所有的人,所以我只會回答我自己的問題,感謝一個大的感謝。

在我的新形式中,我寫了自己的處理腳本,其格式與Ferdinand的建議相同。

$ value = $ _POST ['value'];

我一直在尋找一些舊的東西,一直沿用至今,並試圖找出它的所作所爲,爲什麼呢,以確定是否我在我的新代碼以不安全的快捷方式。事實上,事實恰恰相反,我需要更新舊代碼以使用現在使用的相同方法......並且需要以其他方式修復任何中斷。 Globals的安全問題不會在我身上丟失。

令人驚訝的是,在Google的20頁關於「pt_register」的結果中,我發現了使用完全相同功能的人(我想知道它最初來自哪裏),並且沒有提到它的內在風險/惡意。

感謝大家的即時答覆和有用的信息。這個網站有一個新粉絲。

2

誰說,這是爲註冊全局替換的人在正確的軌道上,但是這並不完全發生了什麼事情。

當恐龍漫遊互聯網時,PHP沒有$ _POST,$ _GET,$ _COOKIE,$ _SESSION,$ _SERVER,$ _ETC超級全局變量。相反,(除了臭名昭著的註冊全局)有一些常規的全局數組命名

$HTTP_GET_VARS 
$HTTP_POST_VARS 
$HTTP_SERVER_VARS 
etc.. 

這些陣列,除了有unweildy長的名字,需要加以聲明的全局(即它們wern't「超級/在使用前自動全局像_POST,$ _ GET等)。

什麼pt_regsiter確實是讓你從這些陣列到全球範圍內的一個導出個人變量。因此,而不是寫

global  $HTTP_SERVER_VARS 
global  $php_self; 
$php_self = $HTTP_SERVER_VARS['PHP_SELF'] 

你可以寫。

pt_register('SERVER','PHP_SELF'); 

而且你會在全局範圍內有一個$ php_self變量。

其他人已經對特定的機制進行了評論,所以我會說,今天使用這個功能會很愚蠢。

然而,它看起來像它起源於一些GNU代碼back in 2002。鑑於PHP3仍被廣泛使用(人們正在過渡到PHP4),這個功能實際上是有意義的。

當時大多數PHP編碼模式都依賴於全局命名空間來完成任務。這個函數實際上有助於限制register_globals的問題。根據上述鏈接中的註釋,作者希望您關閉register_globals,並使用此函數僅導出所需的項目。

邪惡?不是,在這個時代使用令人難以置信的愚蠢?是

0

它可能最初來自於其他地方,但它包含在由phpFormGenerator生成的代碼。

不確定最新版本是否使用不同的代碼,但(很多?)託管公司仍然提供通過Fantastico訪問舊的,這就是爲什麼它仍然被廣泛發現。