我的一亩三分地 我就喜欢!
13fen  设为主页
 收藏本站
 
当前位置: > 一亩三分地:首页 > 网络学院 > 网络编程 > CGI专区 > CGI教程:错误脚本
热门文章排行
热门文章排行 CGI教程:记录用户记录脚本之二(10-27)
跟我学Perl(7)(10-30)
CGI教程:调用CGI脚本文件的例子之四(10-27)
CGI教程:记录用户记录脚本之一(10-27)
跟我学Perl(4)(10-30)
精采文章排行
精采文章排行 实战 FastCGI_2. 安装 FastCGI之二(10-30)
实战 FastCGI_2. 安装 FastCGI之一(10-30)
实战 FastCGI_1.克服 CGI 的瓶颈(3)(10-30)
实战 FastCGI_1.克服 CGI 的瓶颈(2)(10-30)
实战 FastCGI_1.克服 CGI 的瓶颈(1)(10-30)
技术专题推荐
网管论坛交流
 

CGI教程:错误脚本 

作者:   来源:   点击:   日期:2006-10-27

错误脚本 额外的环境变量包括CGI 1.1 变量传递给错误脚本。这些CGI 1.1 变量有:

REDIRECT_REQUEST :这是当正确发送给服务器的请求。

REDIRECT_URL :这是导致错误的请求URL。

REDIRECT_STATUS :这是如果NCSA HTTPd 已经允许应答的状态数字和信息。

另外,NCSA HTTPd在err_string=error_message时作为NCSA HTTPd产生的QUERY_STRING错误字符串传递。一些错误信息可以需要一些不在CGI规范中的文件头。基于这个原因,以下给出一个没有解析的文件头的脚本,它是用Perl编写的:

#!/usr/local/bin/perl

# 这是一个利用Perl编写的没有解析文件头的CGI 1.1错误脚本来处理错误请求。

$error = $ENV{'QUERY_STRING'};

$redirect_request = $ENV{'REDIRECT_REQUEST'};

($redirect_method,$request_url,$redirect_protocal) = split(' ',$redirect_request);

$redirect_status = $ENV{'REDIRECT_STATUS'};

if (!defined($redirect_status)) {

$redirect_status = "200 Ok";

}

($redirect_number,$redirect_message) = split(' ',$redirect_status);

$error =~ s/error=//;



$title = "<HEAD><TITLE>".$redirect_status."</TITLE></HEAD>";



if ($redirect_method eq "HEAD") {

$head_only = 1;

} else {

$head_only = 0;

}



printf("%s %s\r\n",$ENV{'SERVER_PROTOCOL'},$redirect_status);

printf("Server: %s\r\n",$ENV{'SERVER_SOFTWARE'});

printf("Content-type: text/html\r\n");



$redirect_status = "<img alt=\"\" src=/images/icon.gif>".$redirect_status;

if ($redirect_number == 302) {

if ($error !~ /http:/) {

printf("xLocation: http://%s:%s%s\r\n",

$ENV{'SERVER_NAME'},

$ENV{'SERVER_PORT'},

$error);

if (!$head_only) {

printf("%s\r\n",$title);

printf("<H1>%s</H1>\r\n",$redirect_status);

printf("This document has moved");

printf("<A HREF=\"http://%s:%s%s\">here</A>.\r\n",

$ENV{'SERVER_NAME'},

$ENV{'SERVER_PORT'},

$error);

}

} else {

printf("Location: %s\r\n",$error);

if (!$head_only) {

printf("%s\r\n",$title);

printf("<H1>%s</H1>\r\n",$redirect_status);

printf("This document has moved");

printf("<A HREF=\"%s\">here</A>.\r\n",$error);

}

}

} elsif ($redirect_number == 400) {

printf("\r\n");

if (!$head_only) {

printf("%s\r\n",$title);

printf("<H1>%s</H1>\r\n",$redirect_status);

printf("Your client sent a request that this server didn't");

printf(" understand.<br><b>Reason:</b> %s\r\n",$error);

}

} elsif ($redirect_number == 401) {

printf("WWW-Authenticate: %s\r\n",$error);

printf("\r\n");

if (!$head_only) {

printf("%s\r\n",$title);

printf("<H1>%s</H1>\r\n",$redirect_status);

printf("Browser not authentication-capable or ");

printf("authentication failed.\r\n");

}

} elsif ($redirect_number == 403) {

printf("\r\n");

if (!$head_only) {

printf("%s\r\n",$title);

printf("<H1>%s</H1>\r\n",$redirect_status);

printf("Your client does not have permission to get");

printf("URL:%s from this server.\r\n",$ENV{'REDIRECT_URL'});

}

} elsif ($redirect_number == 404) {

printf("\r\n");

if (!$head_only) {

printf("%s\r\n",$title);

printf("<H1>%s</H1>\r\n",$redirect_status);

printf("The requested URL:<code>%s</code> ",

$ENV{'REDIRECT_URL'});

printf("was not found on this server.\r\n");

}

} elsif ($redirect_number == 500) {

printf("\r\n");

if (!$head_only) {

printf("%s\r\n",$title);

printf("<H1>%s</H1>\r\n",$redirect_status);

printf("The server encountered an internal error or ");

printf("misconfiguration and was unable to complete your ");

printf("request \"<code>%s</code>\"\r\n",$redirect_request);

}

} elsif ($redirect_number == 501) {

printf("\r\n");

if (!$head_only) {

printf("%s\r\n",$title);

printf("<H1>%s</H1>\r\n",$redirect_status);

printf("The server is unable to perform the method ");

printf("<b>%s</b> at this time.",$redirect_method);

}

} else {

printf("\r\n");

if (!$head_only) {

printf("%s\r\n",$title);

printf("<H1>%s</H1>\r\n",$redirect_status);

}

}



if (!$head_only) {

printf("<p>The following might be useful in determining the problem:");

printf("<pre>\r\n");

open(ENV,"env|");

while (<ENV>) {

printf("$_");

}

close(ENV);

printf("</pre>\r\n<hr>");

printf("<A HREF=\"http://%s:%s/\"><img alt=\"[Back to Top]\" src=\"/images/back.gif\"> Back to Root of Server</A>\r\n",

$ENV{'SERVER_NAME'},$ENV{'SERVER_PORT'});

printf("<hr><i><a href=\"mailto:webmaster\@%s\">webmaster\@%s</a></i> / ",

$ENV{'SERVER_NAME'},$ENV{'SERVER_NAME'});

printf("<i><a href=\"mailto:httpd\@ncsa.uiuc.edu\">httpd\@ncsa.uiuc.edu</a></i>");

printf("\r\n");


文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·CGI教程:编写安全的CGI脚本 ·CGI教程:ErrorDocument 指令
·CGI教程:记录用户记录脚本之二 ·CGI教程:记录用户记录脚本之一
·CGI教程:调用CGI脚本文件的例子之四 ·CGI教程:调用CGI脚本文件的例子之三

   文章评论:(条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:一分  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。