如何确定是否从命令行或作为cgi脚本调用脚本? [英] How can I determine if a script was called from the command line or as a cgi script?

查看:145
本文介绍了如何确定是否从命令行或作为cgi脚本调用脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我写的脚本,可以在命令行中使用或作为一个CGI脚本,并需要确定如何调用该脚本,以便我可以输出一个内容类型的头部的Web请求(也许一些反缓存头)。我的第一个想法是检查http环境变量的存在:

I have a script that I wrote that can either be used on the command line or as a CGI script, and need to determine how the script was called so I can output a content-type header for web requests (and maybe some anti-cache headers too). My first thought is to check for the existance of http environment variables:

my $js = build_javascript();

if ( exists $ENV{HTTP_HOST} ) {
   print "Content-type: text/javascript\n\n";
}
print $js;

推荐答案



< >解决方案

解决方案

根据 RFC3875 中的CGI规范(第4.1节.4。),GATEWAY_INTERFACE环境变量将是检查你是否在CGI上下文中运行的权威的东西:

According to the CGI specification in RFC3875 (section 4.1.4.), the GATEWAY_INTERFACE environment variable would be the authoritative thing to check whether you are running in a CGI context:


4.1.4 。 GATEWAY_INTERFACE



GATEWAY_INTERFACE 变量必须设置为方言CGI
被服务器用来与脚本通信。

4.1.4. GATEWAY_INTERFACE

The GATEWAY_INTERFACE variable MUST be set to the dialect of CGI being used by the server to communicate with the script.

这篇关于如何确定是否从命令行或作为cgi脚本调用脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆