<!--StartFragment-->我们在一些留言本、BBS讨论区发贴时常会出现这种现象:当进入发贴界面时在要求填写用户名和密码的地方就已经自动地填上了你的资料。这是什么会事呢?这是因为程序中引入了COOKIE技术的缘故。原来在你第一次登录时,程序就已在你的电脑中安装了一个COOKIE信息包,在你今后登陆时电脑就自动检索你的COOKIE并取出信息包的信息供程序调用,所以出现上面所述的现象。
COOKIE只不过是CGI程序要求浏览器持有的一个信息包,这个信息包可以由CGI程序在任何时候收回。每当CGI程序要求创建COOKIE 时,COOKIE可以从服务器传送到浏览器所属的子目录下(通常是保存在C:\WINDOWS\Temporary Internet Files的目录下,这个过程称为COOKIE的安装。
COOKIE的安装和读出通常都由一个CGI模块来完成,下面我就将此模块的原代码公布给大家,有了这个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="gzdzw.51.net"
secure: 如果给出此标志,表示cookie只能通过SSL协议的https服务器来传递。
cookie的接收是通过设置环境变量HTTP_COOKIE来实现的,CGI程序可以通过检索该变量获取cookie信息。
以下是一个cookie的模块,可以为你编写程序带来方便
$Cookie_Exp_Date = '';#此处设置cookie的有效时间
$Cookie_Path = '';#此处设置cookie的有效路径,默认脚本存在的目录。
$Cookie_Domain = ''; #此处设置cookie的有效域名,默认脚本调用的域名。
$Secure_Cookie = '0';
@Cookie_Encode_Chars = ('\%', '\+', '\;', '\,', '\=', '\&', '\:\:', '\s');
%Cookie_Encode_Chars = ('\%', '%25',
'\+', '%2B',
'\;', '%3B',
'\,', '%2C',
'\=', '%3D',
'\&', '%26',
'\:\:', '%3A%3A',
'\s', '+');# 特殊字符的解码
@Cookie_Decode_Chars = ('\+', '\%3A\%3A', '\%26', '\%3D', '\%2C', '\%3B', '\%2B', '\%25');
%Cookie_Decode_Chars = ('\+', ' ',
'\%3A\%3A', '::',
'\%26', '&',
'\%3D', '=',
'\%2C', ',',
'\%3B', ';',
'\%2B', '+',
'\%25', '%');# 特殊字符的解码
#########获取cookie信息子程序#####################
sub GetCookies {
local(@ReturnCookies) = @_;
local($cookie_flag) = 0;
local($cookie,$value);
if ($ENV{'HTTP_COOKIE'}) {
if ($ReturnCookies[0] ne '') {
foreach (split(/; /,$ENV{'HTTP_COOKIE'})) {#分割HTTP_COOKIE变量的字符串
($cookie,$value) = split(/=/);
foreach $char (@Cookie_Decode_Chars) {
$cookie =~ s/$char/$Cookie_Decode_Chars{$char}/g;#调用转码函数
$value =~ s/$char/$Cookie_Decode_Chars{$char}/g;#调用转码函数
}
foreach $ReturnCookie (@ReturnCookies) {
if ($ReturnCookie eq $cookie) {
$Cookies{$cookie} = $value;
$cookie_flag = "1";
}
}
}
}
else {
foreach (split(/; /,$ENV{'HTTP_COOKIE'})) {
($cookie,$value) = split(/=/);
foreach $char (@Cookie_Decode_Chars) {
$cookie =~ s/$char/$Cookie_Decode_Chars{$char}/g;
$value =~ s/$char/$Cookie_Decode_Chars{$char}/g;
}
$Cookies{$cookie} = $value;
}
$cookie_flag = 1;
}
}
return $cookie_flag;
}
########## 设置Cookie信息子程序#######################
sub SetSecureCookie {
if ($_[0] =~ /^[01]$/) {
$Secure_Cookie = $_[0];
return 1;
}
else {
return 0;
}
}
sub SetCookies {
local(@cookies) = @_;
local($cookie,$value,$char);
while( ($cookie,$value) = @cookies ) {
foreach $char (@Cookie_Encode_Chars) {
$cookie =~ s/$char/$Cookie_Encode_Chars{$char}/g;#调用转码函数
$value =~ s/$char/$Cookie_Encode_Chars{$char}/g;#调用转码函数
}
print 'Set-Cookie: ' . $cookie . '=' . $value . ';';#设置cookie值
if ($Cookie_Exp_Date) { print ' expires=' . $Cookie_Exp_Date . ';'; }#设置有效时间
if ($Cookie_Path) { print ' path=' . $Cookie_Path . ';'; }#设置有效路径
if ($Cookie_Domain) { print ' domain=' . $Cookie_Domain . ';'; }#设置有效域名
if ($Secure_Cookie) { print ' secure'; }
print "\n";
shift(@cookies); shift(@cookies);
}
}
sub SetCompressedCookies {
local($cookie_name,@cookies) = @_;
local($cookie,$value,$cookie_value);
while ( ($cookie,$value) = @cookies ) {
foreach $char (@Cookie_Encode_Chars) {
$cookie =~ s/$char/$Cookie_Encode_Chars{$char}/g;
$value =~ s/$char/$Cookie_Encode_Chars{$char}/g;
}
if ($cookie_value) { $cookie_value .= '&' . $cookie . '::' . $value; }
else { $cookie_value = $cookie . '::' . $value; }
shift(@cookies); shift(@cookies);
}
&SetCookies("$cookie_name","$cookie_value");
}
sub GetCompressedCookies {
local($cookie_name,@ReturnCookies) = @_;
local($cookie_flag) = 0;
local($ReturnCookie,$cookie,$value);
if (&GetCookies($cookie_name)) {
if ($ReturnCookies[0] ne '') {
foreach (split(/&/,$Cookies{$cookie_name})) {
($cookie,$value) = split(/::/);
foreach $char (@Cookie_Decode_Chars) {
$cookie =~ s/$char/$Cookie_Decode_Chars{$char}/g;
$value =~ s/$char/$Cookie_Decode_Chars{$char}/g;
}
foreach $ReturnCookie (@ReturnCookies) {
if ($ReturnCookie eq $cookie) {
$Cookies{$cookie} = $value;
$cookie_flag = 1;
}
}
}
}
else {
foreach (split(/&/,$Cookies{$cookie_name})) {
($cookie,$value) = split(/::/);
foreach $char (@Cookie_Decode_Chars) {
$cookie =~ s/$char/$Cookie_Decode_Chars{$char}/g;
$value =~ s/$char/$Cookie_Decode_Chars{$char}/g;
}
$Cookies{$cookie} = $value;
}
$cookie_flag = 1;
}
delete($Cookies{$cookie_name});
}
return $cookie_flag;
}
########结束#################
下面是cookie信息包安装代码段
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE
expireDate = new Date;
if (expireDate.getYear() < 100) {
expireDate.setYear(expireDate.getYear() + 1900 + 1);
} else {
expireDate.setYear(expireDate.getYear()+1);
}
var username = getCookie("UserName");
var password = getCookie("Password");
if (password == null) {
var CookieName = "$username";
var CookiePW = "$userpsd";
if (password == null) {
document.cookie = "username=" + CookieName + ";expires=" + expireDate.toGMTString() + ";";
document.cookie = "password=" + CookiePW + ";expires=" + expireDate.toGMTString() + ";";
}
}
function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return dc.substring(begin, end); }
}
return null;
}
// STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>
###########结束#####################
下面是读取信息包代码,很简单:
&GetCookies;
$name=$Cookies{'username'};
$pass=$Cookies{'password'};