Perl Cgi 主要函数功能简介
2013-08-16 16:38:25 阿炯

这里主要介绍三个比较入时和'现代'的导出函数标签,它们在后端处理动态应用,下面就其用法做一个简单的介绍。可供导入的方法类有如下:

:cgi
Import all CGI-handling methods, such as param(), path_info() and the like.
导入所有的cgi处理方法,像param(), path_info(),主要用于处理动态内容。

:form
Import all fill-out form generating methods, such as textfield().
导入与表单处理的函数方法,如textfield()。

:html2
Import all methods that generate HTML 2.0 standard elements.

:html3
Import all methods that generate HTML 3.0 elements (such as <table>, <super> and <sub>).

:html4
Import all methods that generate HTML 4 elements (such as <abbrev>, <acronym> and <thead>).

:netscape
Import the <blink>, <fontsize> and <center> tags.

:html
Import all HTML-generating shortcuts (i.e. 'html2', 'html3', 'html4' and 'netscape')

:standard
Import "standard" features, 'html2', 'html3', 'html4', 'form' and 'cgi'.
标准方法属性,包括了('html2', 'html3', 'html4', 'form' and 'cgi')。

:all
Import all the available methods. For the full list, see the CGI.pm code, where the variable %EXPORT_TAGS is defined.
导入所有的方法函数,其中所支持方法可以从CGI.pm代码中的'%EXPORT_TAGS'所定义的找到。

建议:在当今流行的MVC开发模式的思路影响下,再将动静态内容混写的模式用来开发就显得不舍时宜了。因此只建议用于处理后端的动态内容,所以导入':cgi'就足够了。

CGI程序中HTTP头部经常使用的信息:
描述
Content-type:请求的与实体对应的MIME信息,例如:Content-type:text/html
Expires: Date响应过期的日期和时间
Location: URL用来重定向接收方到非请求URL的位置来完成请求或标识新的资源
Last-modified: Date请求资源的最后修改时间
Content-length: N请求的内容长度
Set-Cookie: String设置Http Cookie


CGI环境变量
变量名描述
CONTENT_TYPE这个环境变量的值指示所传递来的信息的MIME类型。目前环境变量CONTENT_TYPE一般都是:application/x-www-form-urlencoded,他表示数据来自于HTML表单。
CONTENT_LENGTH如果服务器与CGI程序信息的传递方式是POST,这个环境变量即使从标准输入STDIN中可以读到的有效数据的字节数。这个环境变量在读取所输入的数据时必须使用。
HTTP_COOKIE客户机内的 COOKIE 内容。
HTTP_USER_AGENT提供包含了版本数或其他专有数据的客户浏览器信息。
PATH_INFO这个环境变量的值表示紧接在CGI程序名之后的其他路径信息。它常常作为CGI程序的参数出现。
QUERY_STRING如果服务器与CGI程序信息的传递方式是GET,这个环境变量的值即使所传递的信息。这个信息经跟在CGI程序名的后面,两者中间用一个问号'?'分隔。
REMOTE_ADDR这个环境变量的值是发送请求的客户机的IP地址,例如上面的192.168.1.7。这个值总是存在的。而且它是Web客户机需要提供给Web服务器的唯一标识,可以在CGI程序中用它来区分不同的Web客户机。
REMOTE_HOST这个环境变量的值包含发送CGI请求的客户机的主机名。如果不支持你想查询,则无需定义此环境变量。
REQUEST_METHOD提供脚本被调用的方法。对于使用 HTTP/1.0 协议的脚本,仅 GET 和 POST 有意义。
SCRIPT_FILENAMECGI脚本的完整路径
SCRIPT_NAMECGI脚本的的名称
SERVER_NAME这是你的 WEB 服务器的主机名、别名或IP地址。
SERVER_SOFTWARE这个环境变量的值包含了调用CGI程序的HTTP服务器的名称和版本号。例如上面的值为Apache/2.2.16(Unix)


:cgi
param
用于获取传入的参数(get与post方法),像:cgi.psp?a=1&b=2,来取得并处理。可以使用Dump方法将具体的参数打印到页面上

upload
用于处理文件上传的功能函数,需要为其传入'form'表单中所定义的'<input type="file" name="alertsfile" />','name'的值

path_info
取得相关的路径信息,多与url()函数相关

path_translated
暂不知其用途

request_uri
取得目前正在访问的目录及文件:/url/file.pl?a=1

url
即浏览器里的全部地址,不含传入的参数

self_url
即浏览器里的全部地址,含传入的参数

script_name
顾名思义,返回网站根下的目录文件名称

cookie
设置发送到客户浏览器的'cookie'信息

Dump
按'ul->li'方式打印出所传入参数

raw_cookie
Returns the HTTP_COOKIE variable. Cookies have a special format, and this method call just returns the raw form (?cookie dough). See cookie() for ways of setting and retrieving cooked cookies.

Called with no parameters, raw_cookie() returns the packed cookie structure. You can separate it into individual cookies by splitting on the character sequence "; ". Called with the name of a cookie, retrieves the unescaped form of the cookie. You can use the regular cookie() method to get the names, or use the raw_fetch() method from the CGI::Cookie module.

对从浏览器递交过来的cookie进行处理的函数

request_method
请求的方式,一般为get、post、head等

query_string
传入的参数对,将按新的格式输出:a=1;b=2;c=3,以取代老的以'&'连接起来的格式

Accept
Return a list of MIME types that the remote browser accepts. If you give this method a single argument corresponding to a MIME type, as in Accept('text/html'), it will return a floating point value corresponding to the browser's preference for this type from 0.0 (don't want) to 1.0. Glob types (e.g. text/*) in the browser's accept list are handled correctly.

Note that the capitalization changed between version 2.43 and 2.44 in order to avoid conflict with Perl's accept() function.
即浏览器可接受的文件格式类型

user_agent
返回浏览器的发行厂商、引擎类型、版本信息、操作系统简单信息等

remote_host
用户访问过来的ip地址,不一定是其本地ip地址

content_type
Returns the content_type of data submitted in a POST, generally multipart/form-data or application/x-www-form-urlencoded
返回用户向服务器提交的数据类型,一般是经post过来的数据流格式

remote_addr
用户本地的ip地址

referer
Return the URL of the page the browser was viewing prior to fetching your script. Not available for all browsers.
即从其它页面跳过来,也可以为是上一次浏览的页面地址

server_name
虚拟主机名称

server_software
web服务器的简单信息

server_port
站点所在服务器的端口信息

server_protocol
所用协议,一般为'http/1.1'

virtual_port
virtual_host
这两项与上同

remote_ident
暂不清楚它的含义

auth_type
如果服务器支持基本的认证并且如果脚本被保护,此变量提供认证类型,此信息是特定于协议和服务器的

http
目前包含了8个相关的http属性:HTTP_REFERER、HTTP_ACCEPT_ENCODING、HTTP_CONNECTION、HTTP_ACCEPT、HTTP_USER_AGENT、HTTP_CACHE_CONTROL、HTTP_ACCEPT_LANGUAGE、HTTP_HOST
这其中的参考信息不多,具体的信息可从环境变量中获取

append
This adds a value or list of values to the named parameter. The values are appended to the end of the parameter if it already exists. Otherwise the parameter is created. Note that this method only recognizes the named argument calling syntax.
对传入参数进行追加修改

save_parameters
restore_parameters
对传入的参数进行保存或导出,但不知这两个功能函数具体有何用处

param_fetch
直接访问处理参数列表,This will return an array reference to the named parameter, which you then can manipulate in any way you like.

remote_user
user_name
取得与http环境相关的用户信息

header
输出定义的http头信息,当然也可以包含html head信息

redirect
print $q->redirect(-uri=>'http://somewhere.else/in/movie/land',-nph=>1,-status=>'301 Moved Permanently');

All names arguments recognized by header() are also recognized by redirect(). However, most HTTP headers, including those generated by -cookie and -target, are ignored by the browser.

The -nph parameter, if set to a true value, will issue the correct headers to work with a NPH (no-parse-header) script. This is important to use with certain servers, such as Microsoft IIS, which expect all their scripts to be NPH.

The -status parameter will set the status of the redirect. HTTP defines three different possible redirection status codes:
 301 Moved Permanently
 302 Found
 303 See Other

The default if not specified is 302, which means "moved temporarily." You may change the status to another status code if you wish. Be advised that changing the status to anything other than 301, 302 or 303 will probably break redirection.

这些重定向在浏览器下已经不能正常生效了,可以考虑其它方法

import_names
将所有的参数导入到一个名称空间里,不知有何用处

put
put 方法,把消息本体中的消息发送到一个UR。跟POST类似,但不常用。
简单地说:通常用于向服务器发送请求,如果URI不存在,则要求服务器根据请求创建资源,如果存在,服务器就接受请求内容,并修改URI资源的原始版本。

delete
将传入的参数列表的key删除

delete_all
将传入的参数列表清空

url_param
取得参数列表内容:'key=value'中的'key'的值,当然也可传入具体的key取得相应值

cgi_error
对处理过程出现了的错误进行处理
$file = $q->upload('uploaded_file');
if (!$file && $q->cgi_error) {
 print $q->header(-status=>$q->cgi_error);
 exit 0;
}

:ssl
https
The same as http(), but operates on the HTTPS environment variables present when the SSL protocol is in effect. Can be used to determine whether SSL is turned on.

使用安全的http连接

:push
multipart_init
multipart_start
multipart_end
multipart_final

http push技术在现在看来显得有些过时,这里不再介绍。


相关的技术术语说明

HTTP Header

The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the browser to understand the content. All the HTTP header will be in the following form

HTTP Field Name: Field Content

For Example
Content-type:text/html\r\n\r\n

There are few other important HTTP headers which you will use frequently in your CGI Programming.

S.No.Header & Description
1

Content-type: String

A MIME string defining the format of the file being returned. Example is Content-type:text/html

2

Expires: Date String

The date the information becomes invalid. This should be used by the browser to decide when a page needs to be refreshed. A valid date string should be in the format 01 Jan 1998 12:00:00 GMT.

3

Location: URL String

The URL that should be returned instead of the URL requested. You can use this filed to redirect a request to any file.

4

Last-modified: String

The date of last modification of the resource.

5

Content-length: String

The length, in bytes, of the data being returned. The browser uses this value to report the estimated download time for a file.

6

Set-Cookie: String

Set the cookie passed through the string

CGI Environment Variables

All the CGI program will have access to the following environment variables. These variables play an important role while writing any CGI program.

S.No.Variable Name & Description
1

CONTENT_TYPE

The data type of the content. Used when the client is sending attached content to the server. For example file upload etc.

2

CONTENT_LENGTH

The length of the query information. It's available only for POST requests.

3

HTTP_COOKIE

Return the set cookies in the form of key & value pair.

4

HTTP_USER_AGENT

The User-Agent request-header field contains information about the user agent originating the request. Its name of the web browser.

5

PATH_INFO

The path for the CGI script.

6

QUERY_STRING

The URL-encoded information that is sent with GET method request.

7

REMOTE_ADDR

The IP address of the remote host making the request. This can be useful for logging or for authentication purpose.

8

REMOTE_HOST

The fully qualified name of the host making the request. If this information is not available then REMOTE_ADDR can be used to get IR address.

9

REQUEST_METHOD

The method used to make the request. The most common methods are GET and POST.

10

SCRIPT_FILENAME

The full path to the CGI script.

11

SCRIPT_NAME

The name of the CGI script.

12

SERVER_NAME

The server's hostname or IP Address.

13

SERVER_SOFTWARE

The name and version of the software the server is running.

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<font size=+1>Environment</font>\n";

foreach (sort keys %ENV) {
   print "<b>$_</b>: $ENV{$_}<br>\n";
}

1;

Output

Environment CONTEXT_DOCUMENT_ROOT: 
CONTEXT_PREFIX: 
DOCUMENT_ROOT: 
GATEWAY_INTERFACE: 
GEOIP_ADDR: 
GEOIP_CONTINENT_CODE: 
GEOIP_COUNTRY_CODE: 
GEOIP_COUNTRY_NAME: 
HTTP_ACCEPT: 
HTTP_ACCEPT_ENCODING: 
HTTP_ACCEPT_LANGUAGE: 
HTTP_COOKIE: 
HTTP_HOST: 
HTTP_UPGRADE_INSECURE_REQUESTS: 
HTTP_USER_AGENT: 
HTTP_VIA: 
HTTP_X_FORWARDED_FOR: 
HTTP_X_FORWARDED_PROTO: 
HTTP_X_HOST: 
PATH: 
QUERY_STRING: 
REMOTE_ADDR: 
REMOTE_PORT: 
REQUEST_METHOD: 
REQUEST_SCHEME: 
REQUEST_URI: 
SCRIPT_FILENAME: 
SCRIPT_NAME: 
SCRIPT_URI: 
SCRIPT_URL: 
SERVER_ADDR: 
SERVER_ADMIN: 
SERVER_NAME: 
SERVER_PORT: 
SERVER_PROTOCOL: 
SERVER_SIGNATURE: 
SERVER_SOFTWARE: 
UNIQUE_ID: 

Using Cookies in CGI

HTTP protocol is a stateless protocol. But for a commercial website it is required to maintain session information among different pages. For example one user registration ends after completing many pages. But how to maintain user's session information across all the web pages.

In many situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics.

How It Works

Your server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie. If it does, it is stored as a plain text record on the visitor's hard drive. Now, when the visitor arrives at another page on your site, the cookie is available for retrieval. Once retrieved, your server knows/remembers what was stored.

Cookies are a plain text data record of 5 variable-length fields -

Expires - The date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the browser.

Domain - The domain name of your site.

Path - The path to the directory or web page that set the cookie. This may be blank if you want to retrieve the cookie from any directory or page.

Secure - If this field contains the word "secure" then the cookie may only be retrieved with a secure server. If this field is blank, no such restriction exists.

Name=Value − Cookies are set and retrviewed in the form of key and value pairs.


使用Cookie

在 http 协议一个很大的缺点就是不对用户身份的进行判断,这样给编程人员带来很大的不便,而 cookie 功能的出现弥补了这个不足。cookie 就是在客户访问脚本的同时,通过客户的浏览器,在客户硬盘上写入纪录数据 ,当下次客户访问脚本时取回数据信息,从而达到身份判别的功能,cookie 常用在身份校验中。
 
cookie的语法

http cookie的发送是通过http头部来实现的,早于文件的传递,头部set-cookie的语法如下:
Set-cookie:name=name;expires=date;path=path;domain=domain;secure

name=name: 需要设置cookie的值(name不能使用";"和","号),有多个name值时用 ";" 分隔,例如:name1=name1;name2=name2;name3=name3。
expires=date: cookie的有效期限,格式: expires="Wdy,DD-Mon-YYYY HH:MM:SS"
path=path: 设置cookie支持的路径,如果path是一个路径,则cookie对这个目录下的所有文件及子目录生效,例如: path="/cgi-bin/",如果path是一个文件,则cookie指对这个文件生效,例如:path="/cgi-bin/cookie.cgi"。
domain=domain: 对cookie生效的域名,例如:domain="www.runoob.com"
secure: 如果给出此标志,表示cookie只能通过SSL协议的https服务器来传递。
cookie的接收是通过设置环境变量HTTP_COOKIE来实现的,CGI程序可以通过检索该变量获取cookie信息。

Cookie的设置非常简单,cookie会在http头部单独发送。Cookie信息检索页非常简单,Cookie信息存储在CGI的环境变量HTTP_COOKIE中。