赞
踩
CGI叫做通用网关接口,用作前台和后台交互,可以用多种语言中的任何一种来写
The Common Gateway Interface (CGI) is a standard for interfacing external applications with web servers. CGI was originally developed as part of the NCSA HTTP server and is an old standard for interfacing external applications with HTTP servers. It still enjoys considerable use.
CGI was created to allow dynamic data to be generated in response to HTTP requests and return the results to the clients's browser. Plain HTML documents are typically static, while a CGI program allows the response data to be dynamically created.
CGI scripts are written in any language that can read from the standard-input, write to the standard-output, and access environment variables. This means that virtually any programming language can be used, including C, Perl, or even Unix shell scripting.
下面用C语言来表示一个CGI程序
完成这个实例有3个文件,分别是cgic.c / cgic.h / test.c
/*cgic.c cgic.h 就是 cgic库,在我的下载资源中有*/
/*test.c*/
#include <stdio.h>
#include <stdlib.h>
#include "cgic.h"
int cgiMain(int argc, const char *argv[])
{
char * receive_data;
cgiHeaderContentType ("text/html\n\n");
receive_data = getenv("QUERY_STRING");
printf("<p>receive_data : %s</p>",receive_data);
printf("<p>response : %s</p>","hello");
return 0;
}//end of main
/*编译方式*/
gcc test.c cgic.c -o test
只需要一个文件 test.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char * receive_data;
printf("Content-type:text/html;charset=gb2312\n\n");
/*
*wih out the printf code ,it response as belows:
*502 Bad Gateway
*The CGI was not CGI/1.1 compliant.
* */
receive_data = getenv("QUERY_STRING");
printf("<p>receive_data : %s</p>",receive_data);
printf("<p>response : %s</p>","hello");
return 0;
}//end of main
/*编译方式*/
gcc test.c -o test
<!DOCTYPE html>
<html>
<body>
<form action="/test" method="GET">
Name:
<br>
<input type="text" name="name" value="Mickey">
<br>
Passwd:
<br>
<input type="text" name="passwd" value="123456">
<br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。