以下是一个通过HTML的表单使用GET方法向服务器发送两个数据,提交的服务器脚本同样是test.cgi文件,test.html 代码如下:
#!/usr/bin/perl
local
(
$buffer
,
@pairs
,
$pair
,
$name
,
$value
,
%FORM
)
;
$ENV
{
'
REQUEST_METHOD
'
}
=~
tr
/
a
-
z
/
A
-
Z
/;
if
(
$ENV
{
'
REQUEST_METHOD
'
}
eq
"
POST
"
)
read
(
STDIN
,
$buffer
,
$ENV
{
'
CONTENT_LENGTH
'
}
)
;
}
else
{
$buffer
=
$ENV
{
'
QUERY_STRING
'
}
;
@pairs
=
split
(
/&/,
$buffer
)
;
foreach
$pair
(
@pairs
)
(
$name
,
$value
)
=
split
(
/=/,
$pair
)
;
$value
=~
tr
/+/
/
;
$value =~ s
/
%
(
..
)
/
pack
(
"
C
"
,
hex
(
$1
)
)
/
eg
;
$FORM
{
$name
}
=
$value
;
$name
=
$FORM
{
name
}
;
$url
=
$FORM
{
url
}
;
print
"
Content-type:text/html
\r
\n
\r
\n
"
;
print
"
<html>
"
;
print
"
<head>
"
;
print
'
<meta charset="utf-8">
'
;
print
'
<title>菜鸟教程(runoob.com)</title>
'
;
print
"
</head>
"
;
print
"
<body>
"
;
print
"
<h2>
$name
网址:
$url
</h2>
"
;
print
"
</body>
"
;
print
"
</html>
"
;
以下是一个通过HTML的表单使用GET方法向服务器发送两个数据,提交的服务器脚本同样是test.cgi文件,test.html 代码如下:
test.html 代码
<
!
DOCTYPE
html
>
<
meta
charset
=
"
utf-8
"
>
<
title
>
菜鸟教程(runoob.com)
</
title
>
</
head
>
<
form
action
=
"
/cgi-bin/test.cgi
"
method
=
"
post
"
>
站点名称:
<
input
type
=
"
text
"
name
=
"
name
"
>
<
br
/>
站点 URL:
<
input
type
=
"
text
"
name
=
"
url
"
/>
<
input
type
=
"
submit
"
value
=
"
提交
"
/>
</
form
>
</
body
>
</
html
>
浏览器中,执行效果如下所示:
通过CGI程序传递checkbox数据
checkbox用于提交一个或者多个选项数据,test.html 代码如下:
test.html 代码
<
!
DOCTYPE
html
>
<
meta
charset
=
"
utf-8
"
>
<
title
>
菜鸟教程(runoob.com)
</
title
>
</
head
>
<
form
action
=
"
/cgi-bin/test.cgi
"
method
=
"
POST
"
target
=
"
_blank
"
>
<
input
type
=
"
checkbox
"
name
=
"
runoob
"
value
=
"
on
"
/>
菜鸟教程
<
input
type
=
"
checkbox
"
name
=
"
google
"
value
=
"
on
"
/>
Google
<
input
type
=
"
submit
"
value
=
"
选择站点
"
/>
</
form
>
</
body
>
</
html
>
以下为 test.cgi 文件的代码:
test.cgi 代码
#!/usr/bin/perl
local
(
$buffer
,
@pairs
,
$pair
,
$name
,
$value
,
%FORM
)
;
$ENV
{
'
REQUEST_METHOD
'
}
=~
tr
/
a
-
z
/
A
-
Z
/;
if
(
$ENV
{
'
REQUEST_METHOD
'
}
eq
"
POST
"
)
read
(
STDIN
,
$buffer
,
$ENV
{
'
CONTENT_LENGTH
'
}
)
;
}
else
{
$buffer
=
$ENV
{
'
QUERY_STRING
'
}
;
@pairs
=
split
(
/&/,
$buffer
)
;
foreach
$pair
(
@pairs
)
(
$name
,
$value
)
=
split
(
/=/,
$pair
)
;
$value
=~
tr
/+/
/
;
$value =~ s
/
%
(
..
)
/
pack
(
"
C
"
,
hex
(
$1
)
)
/
eg
;
$FORM
{
$name
}
=
$value
;
if
(
$FORM
{
runoob
}
)
{
$runoob_flag
=
"
ON
"
;
}
else
{
$runoob_flag
=
"
OFF
"
;
if
(
$FORM
{
google
}
)
{
$google_flag
=
"
ON
"
;
}
else
{
$google_flag
=
"
OFF
"
;
print
"
Content-type:text/html
\r
\n
\r
\n
"
;
print
"
<html>
"
;
print
"
<head>
"
;
print
'
<meta charset="utf-8">
'
;
print
'
<title>菜鸟教程(runoob.com)</title>
'
;
print
"
</head>
"
;
print
"
<body>
"
;
print
"
<h2> 菜鸟教程选中状态 :
$runoob_flag
</h2>
"
;
print
"
<h2> Google 选择状态 :
$google_flag
</h2>
"
;
print
"
</body>
"
;
print
"
</html>
"
;
浏览器中,执行效果如下所示:
<
form
action
=
"
/cgi-bin/test.cgi
"
method
=
"
post
"
target
=
"
_blank
"
>
<
input
type
=
"
radio
"
name
=
"
site
"
value
=
"
runoob
"
/>
菜鸟教程
<
input
type
=
"
radio
"
name
=
"
site
"
value
=
"
google
"
/>
Google
<
input
type
=
"
submit
"
value
=
"
提交
"
/>
</
form
>
</
body
>
</
html
>
test.cgi 脚本代码如下:
test.cgi 代码
#!/usr/bin/perl
local
(
$buffer
,
@pairs
,
$pair
,
$name
,
$value
,
%FORM
)
;
$ENV
{
'
REQUEST_METHOD
'
}
=~
tr
/
a
-
z
/
A
-
Z
/;
if
(
$ENV
{
'
REQUEST_METHOD
'
}
eq
"
POST
"
)
read
(
STDIN
,
$buffer
,
$ENV
{
'
CONTENT_LENGTH
'
}
)
;
}
else
{
$buffer
=
$ENV
{
'
QUERY_STRING
'
}
;
@pairs
=
split
(
/&/,
$buffer
)
;
foreach
$pair
(
@pairs
)
(
$name
,
$value
)
=
split
(
/=/,
$pair
)
;
$value
=~
tr
/+/
/
;
$value =~ s
/
%
(
..
)
/
pack
(
"
C
"
,
hex
(
$1
)
)
/
eg
;
$FORM
{
$name
}
=
$value
;
$site
=
$FORM
{
site
}
;
print
"
Content-type:text/html
\r
\n
\r
\n
"
;
print
"
<html>
"
;
print
"
<head>
"
;
print
'
<meta charset="utf-8">
'
;
print
'
<title>菜鸟教程(runoob.com)</title>
'
;
print
"
</head>
"
;
print
"
<body>
"
;
print
"
<h2> 选择的网站
$site
</h2>
"
;
print
"
</body>
"
;
print
"
</html>
"
;
浏览器中,执行效果如下所示:
通过CGI程序传递 Textarea 数据
Textarea 向服务器传递多行数据,test.html 代码如下:
test.html 代码
<
!
DOCTYPE
html
>
<
meta
charset
=
"
utf-8
"
>
<
title
>
菜鸟教程(runoob.com)
</
title
>
</
head
>
<
form
action
=
"
/cgi-bin/test.cgi
"
method
=
"
post
"
target
=
"
_blank
"
>
<
textarea
name
=
"
textcontent
"
cols
=
"
40
"
rows
=
"
4
"
>
在这里输入内容...
</
textarea
>
<
input
type
=
"
submit
"
value
=
"
提交
"
/>
</
form
>
</
body
>
</
html
>
test.cgi 脚本代码如下:
test.cgi 代码
#!/usr/bin/perl
local
(
$buffer
,
@pairs
,
$pair
,
$name
,
$value
,
%FORM
)
;
$ENV
{
'
REQUEST_METHOD
'
}
=~
tr
/
a
-
z
/
A
-
Z
/;
if
(
$ENV
{
'
REQUEST_METHOD
'
}
eq
"
POST
"
)
read
(
STDIN
,
$buffer
,
$ENV
{
'
CONTENT_LENGTH
'
}
)
;
}
else
{
$buffer
=
$ENV
{
'
QUERY_STRING
'
}
;
@pairs
=
split
(
/&/,
$buffer
)
;
foreach
$pair
(
@pairs
)
(
$name
,
$value
)
=
split
(
/=/,
$pair
)
;
$value
=~
tr
/+/
/
;
$value =~ s
/
%
(
..
)
/
pack
(
"
C
"
,
hex
(
$1
)
)
/
eg
;
$FORM
{
$name
}
=
$value
;
$text_content
=
$FORM
{
textcontent
}
;
print
"
Content-type:text/html
\r
\n
\r
\n
"
;
print
"
<html>
"
;
print
"
<head>
"
;
print
'
<meta charset="utf-8">
'
;
print
'
<title>菜鸟教程(runoob.com)</title>
'
;
print
"
</head>
"
;
print
"
<body>
"
;
print
"
<h2>输入的文本内容为:
$text_content
</h2>
"
;
print
"
</body>
"
;
print
"
</html>
"
;
浏览器中,执行效果如下所示:
通过 CGI 程序传递下拉数据
HTML 下拉框代码如下:
test.html 代码
<
!
DOCTYPE
html
>
<
meta
charset
=
"
utf-8
"
>
<
title
>
菜鸟教程(runoob.com)
</
title
>
</
head
>
<
form
action
=
"
/cgi-bin/test.cgi
"
method
=
"
post
"
target
=
"
_blank
"
>
<
select
name
=
"
dropdown
"
>
<
option
value
=
"
runoob
"
selected
>
菜鸟教程
</
option
>
<
option
value
=
"
google
"
>
Google
</
option
>
</
select
>
<
input
type
=
"
submit
"
value
=
"
提交
"
/>
</
form
>
</
body
>
</
html
>
test.cgi 脚本代码如下所示:
test.cgi 代码
#!/usr/bin/perl
local
(
$buffer
,
@pairs
,
$pair
,
$name
,
$value
,
%FORM
)
;
$ENV
{
'
REQUEST_METHOD
'
}
=~
tr
/
a
-
z
/
A
-
Z
/;
if
(
$ENV
{
'
REQUEST_METHOD
'
}
eq
"
POST
"
)
read
(
STDIN
,
$buffer
,
$ENV
{
'
CONTENT_LENGTH
'
}
)
;
}
else
{
$buffer
=
$ENV
{
'
QUERY_STRING
'
}
;
@pairs
=
split
(
/&/,
$buffer
)
;
foreach
$pair
(
@pairs
)
(
$name
,
$value
)
=
split
(
/=/,
$pair
)
;
$value
=~
tr
/+/
/
;
$value =~ s
/
%
(
..
)
/
pack
(
"
C
"
,
hex
(
$1
)
)
/
eg
;
$FORM
{
$name
}
=
$value
;
$site
=
$FORM
{
dropdown
}
;
print
"
Content-type:text/html
\r
\n
\r
\n
"
;
print
"
<html>
"
;
print
"
<head>
"
;
print
'
<meta charset="utf-8">
'
;
print
'
<title>菜鸟教程(runoob.com)</title>
'
;
print
"
</head>
"
;
print
"
<body>
"
;
print
"
<h2>选择的网站是:
$site
</h2>
"
;
print
"
</body>
"
;
print
"
</html>
"
;
浏览器中,执行效果如下所示:
CGI中使用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的设置非常简单,cookie会在http头部单独发送。以下实例在cookie中设置了UserID、Password 和 expires:
#!/usr/bin/perl
print
"
Set-Cookie:UserID=XYZ;
\n
"
;
print
"
Set-Cookie:Password=XYZ123;
\n
"
;
print
"
Set-Cookie:Expires=Tuesday, 31-Dec-2017 23:12:40 GMT
"
;\
n
"
;
print
"
Set
-
Cookie
:
Domain
=
www
.
runoob
.
com
;\
n
"
;
print
"
Set
-
Cookie
:
Path
=/
perl
;\
n
"
;
print
"
Content
-
type
:
text
/
html
\
r
\
n
\
r
\
n
"
;
...........其他 HTML 内容
查找 Cookie
Cookie信息检索页非常简单,Cookie信息存储在CGI的环境变量HTTP_COOKIE中,存储格式如下:
#!/usr/bin/perl
$rcvd_cookies
=
$ENV
{
'
HTTP_COOKIE
'
}
;
@cookies
=
split
/
;
/
,
$rcvd_cookies
;
foreach
$cookie
(
@cookies
)
{
(
$key
,
$val
)
=
split
(
/=/,
$cookie
)
;
$key
=~
s
/^\
s
+//;
$val
=~
s
/^\
s
+//;
$key
=~
s
/\
s
+$//;
$val
=~
s
/\
s
+$//;
if
(
$key
eq
"
UserID
"
)
{
$user_id
=
$val
;
}
elsif
(
$key
eq
"
Password
"
)
{
$password
=
$val
;
print
"
User ID =
$user_id
\n
"
;
print
"
Password =
$password
\n
"
;
以上实例输出结果为:
User ID = XYZ
Password = XYZ123
CGI 模块
Perl 提供了很多内置的 CGI 模块,常用以下两个:
CGI 模块
Berkeley cgi-lib.pl