CGI实例--表单GET与POST示例
时间:2010-07-29
来源:互联网
本帖最后由 happyboygd 于 2010-07-29 11:03 编辑
CGI实例--表单GET与POST示例
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\" onclick=\"javascript:window.location='../cgi.html'\">");
printf("</div>\n");
printf("</BODY>\n");
printf("</HTML>\n");
return 0;
}
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\" onclick=\"javascript:window.location='../cgi.html'\">");
printf("</div>\n");
printf("</BODY>\n");
printf("</HTML>\n");
fflush(stdout);
return 0;
}
再附上html测试文件cgi.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">
<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">
<input type="txt" size="3" name="m">*
<input type="txt" size="3" name="n">=
<input type="submit" value="resu">
</form>
</td></tr>
<tr><td><inputtype="button" value="Back Home"onclick='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(下面有定义)。
(6) fgets(poststr,len+1,stdin);
这个是关于fgets函数的使用问题,自己可以上网搜索一下,这里不再详述!
CGI实例--表单GET与POST示例
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\" onclick=\"javascript:window.location='../cgi.html'\">");
printf("</div>\n");
printf("</BODY>\n");
printf("</HTML>\n");
return 0;
}
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\" onclick=\"javascript:window.location='../cgi.html'\">");
printf("</div>\n");
printf("</BODY>\n");
printf("</HTML>\n");
fflush(stdout);
return 0;
}
再附上html测试文件cgi.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">
<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">
<input type="txt" size="3" name="m">*
<input type="txt" size="3" name="n">=
<input type="submit" value="resu">
</form>
</td></tr>
<tr><td><inputtype="button" value="Back Home"onclick='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(下面有定义)。
(6) fgets(poststr,len+1,stdin);
这个是关于fgets函数的使用问题,自己可以上网搜索一下,这里不再详述!
作者: happyboygd 发布时间: 2010-07-29
不错,可以作为参考。
作者: ecjtubaowp 发布时间: 2010-07-29

作者: teng0210 发布时间: 2010-07-29
很好,谢谢
作者: linuxlixk 发布时间: 2010-07-29
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28