2010-01-29 74 views
1

對於perl cgi腳本,這兩者之間有什麼區別(技術上)?

#!/usr/bin/perl 
use CGI; 
$cgi = new CGI; 
print $cgi->header(), 
$cgi->start_html(), 
$cgi->pre($cgi->param()), 
$cgi->end_html(); 

#!/usr/bin/perl 
use CGI; 
$cgi = new CGI; 
print $cgi->header(), 
$cgi->start_html(), 
$cgi->pre($ENV{'QUERY_STRING'}), 
$cgi->end_html(); 
+1

請停止使用骯髒的舊CGI.pm.改用現代和乾淨的網絡引擎,例如[舞者](http://www.perldancer.org/)或[Mojolicious](http://mojolicious.org/)。 – dolmen 2012-08-31 10:21:30

回答

4

假設這樣的HTTP請求:

GET my.cgi?foo=bar&baz=buz 

當在具有常規CGI接口的網絡服務器下運行時,環境變量QUERY_STRING將爲foo=bar&baz=buz。該環境變量不會被URL未轉義。與$cgi->pre(...)打印便索性圍成<pre></pre>標籤(或單個<pre />標籤的環境變量,如果該值或者被強制轉換爲空字符串。

$cgi->param(),在另一方面,假設不帶參數列表環境,將返回一個列表網址轉義 CGI參數名稱,在這種情況下foobar

(請注意,$cgi->pre(...) HTML-逃脫它的參數,所以$ENV{QUERY_STRING}可能只是危及你的CG我用一點點跨站腳本注入。)

+0

真的很好解釋,thx! – CatholicEvangelist 2010-01-29 20:08:00

1

param方法的CGI對象上返回所有的查詢參數,包括GET和POST參數的列表。除非傳入參數,否則它會查找具有該名稱的參數並返回值。

QUERY_STRING環境變量包含未分析的查詢字符串。

如果您嘗試了有問題的代碼,這會非常明顯。

Hereparam的文檔。

-1

CGI.pm

#### Method: param 
# Returns the value(s)of a named parameter. 
# If invoked in a list context, returns the 
# entire list. Otherwise returns the first 
# member of the list. 
# If name is not provided, return a list of all 
# the known parameters names available. 
# If more than one argument is provided, the 
# second and subsequent arguments are used to 
# set the value of the parameter. 

QUERY_STRING源是由web服務器將其設置是簡單地從URI查詢字符串:you can read more about it here

相關問題