CGI编程

 

CGI(Common Gateway Interface)是外部扩展应用程序与WWW服务器交互的一个标准接口。按照CGI标准编写的外部扩展应用程序可以处理客户端浏览器输入的数据,从而完成客户端与服务器的交互操作。 通过 CGI可以提供许多静态的Html网页无法实现的功能。
  HTTP协议是WWW的基础,它基于客户/服务器模型。一个服务器可以为分布在网络各处的客户提供服务。它是建立在TCP/IP协议之上的“无连接”协议。每次连接只处理一个请求。当一个请求到来时,便创建一个子进程为用户的连接服务。根据请求的不同,服务器会返回HTML文件或通过CGI凋用外部应用程序,返回处理结果。服务器通过 CGI与外部程序和脚本之问进行交互,根据客户端在进行请求时所采取的方法,服务器会收集客户所提供的信息,并将该部分信息发送给指定的CGI扩展程序。 CGI扩展程序对信息进行处理并将结果返回服务器。服务器对信息进行分析后,将结果发送网客户端。
   
  外部CGI程序与www服务器进行通信、传递有关参数和处理结果是通过环境变量、命令行参数和标准输入来进行的。服务器提供了客户端(浏览器)与CGI扩展程序之问的信息交换的通道。客户的请求通过服务器的标准输出传送给CGI的标准输入。CGI对信息进行处理后,会将结果发回到它的标准输入,然后由服务器将处理结果发送给客户端。

 

    下面举个例子说明CGI的用法:

 

GET方法:做一个加法运算,需要接收两个参数。
文件get.c如下:
-------------------------------
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
        char *data;
        char a[10],b[10];
        printf("Content-Type:text/html/n/n");
        printf("<HTML>/n");
        printf("<HEAD>/n<TITLE >Get Method</TITLE>/n</HEAD>/n");
        printf("<BODY>/n");
        printf("<div style=/"font-size:12px/">/n");
        data = getenv("QUERY_STRING");
        if(sscanf(data,"a=%[^&]&b=%s",a,b)!=2){
                printf("<DIV STYLE=/"COLOR:RED/">Error:Parameters should be entered!</DIV>/n");
        }
        else{
                printf("<DIV STYLE=/"COLOR:GREEN; font-size:15px; font-weight:bold/">a + b = %d</DIV>/n",atoi(a)+atoi(b));
        }
        printf("<HR COLOR=/"blue/" align=/"left/" width=/"100/">");
        printf("<input type=/"button/" value=/"Back CGI/" οnclick=/"javascript:window.location='../index.html'/">");
        printf("</div>/n");
        printf("</BODY>/n");
        printf("</HTML>/n");
        return 0;
}
gcc -o get.cgi get.c

将生成的get.cgi cp到cgi-bin中
POST方法:做一个乘法运算,需要接收两个参数。
文件post.c如下:
--------------------------------
#include <stdio.h>
#include <stdlib.h>
int main(void){
        int len;
        char *lenstr,poststr[20];
        char m[10],n[10];
        printf("Content-Type:text/html/n/n");
        printf("<HTML>/n");
        printf("<HEAD>/n<TITLE >Post Method</TITLE>/n</HEAD>/n");
        printf("<BODY>/n");
        printf("<div style=/"font-size:12px/">/n");
        lenstr=getenv("CONTENT_LENGTH");
        if(lenstr == NULL)
                printf("<DIV STYLE=/"COLOR:RED/">Error:Parameters should be entered!</DIV>/n");
        else{
                len=atoi(lenstr);
                fgets(poststr,len+1,stdin);
                if(sscanf(poststr,"m=%[^&]&n=%s",m,n)!=2){
                        printf("<DIV STYLE=/"COLOR:RED/">Error: Parameters are not right!</DIV>/n");
                }
                else{
                        printf("<DIV STYLE=/"COLOR:GREEN; font-size:15px; font-weight:bold/">m * n = %d</DIV>/n",atoi(m)*atoi(n));
                }
        }
        printf("<HR COLOR=/"blue/" align=/"left/" width=/"100/">");
        printf("<input type=/"button/" value=/"Back CGI/" οnclick=/"javascript:window.location='../index.html'/">");
        printf("</div>/n");
        printf("</BODY>/n");
        printf("</HTML>/n");
        fflush(stdout);
        return 0;
}

gcc -o post.cgi post.c

将生成的post.cgi cp到cgi-bin中
再附上html测试文件index.html:
--------------------------------
<html>
<head>
<title>CGI Testing</title>
</head>
<body>
<table width="200" height="180" border="0" style="font-size:12px">
<tr><td>
<div style="font-weight:bold; font-size:15px">Method: GET</div>
<div>Please input two number:<div>
<form method="get" action="cgi-bin/get.cgi">
<input type="txt" size="3" name="a">+
<input type="txt" size="3" name="b">=
<input type="submit" value="sum">
</form>
</td></tr>
<tr><td>
<div style="font-weight:bold; font-size:15px">Method: POST</div>
<div>Please input two number:<div>
<form method="post" action="cgi-bin/post.cgi">
<input type="txt" size="3" name="m">*
<input type="txt" size="3" name="n">=
<input type="submit" value="resu">
</form>
</td></tr>
<tr><td><input type="button" value="Back Home" οnclick='javascript:window.location="./index.html"'></td></tr>
</table>
</body>
</html>

几点简要说明:
(1)printf("Content-Type:text/html/n/n");
此行通过标准输出将字符串″Contenttype:text/plain/n/n″传送给Web服务器。它是一个MIME头信息,它告诉Web服务器随后的输出是以纯ASCII文本的形式。请注意在这个头信息中有两个换行符,这是因为Web服务器需要在实际的文本信息开始之前先看见一个空行。
(2)data = getenv("QUERY_STRING");
CGI定义:当GET方法提交的表单被发送到服务器断后,表单中的数据被保存在服务器上一个叫做QUERY_STRING的环境变量中。这种表单的处理相对简单,只要读取环境变量就可以了。
(3)sscanf(data,"a=%[^&]&b=%s",a,b)!=2
这个是关于sscanf函数的使用问题,自己可以上网搜索一下,这里不再详述!
(4)atoi(a)+atoi(b)
atoi函数的功能是将字符型成整型,只有转换之后才可以进行加法运算!
(5)lenstr=getenv("CONTENT_LENGTH");
Web服务器在调用使用POST方法的CGI程序时设置此环境变量,它的文本值表示Web服务器传送给CGI程序的输入中的字符数目,因此需要使用函数atoi() 将此环境变量的值转换成整数,并赋给变量len(下面有定义)。

如果要使用中文的话,要在<head> </head>之间加入<meta http-equiv=content-type content="text/html;charset=utf-8">

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值