CGI程序编写
来源:互联网 发布:nginx 防止ddos攻击 编辑:程序博客网 时间:2024/06/04 20:10
本文的内容是:写一个CGI程序。
CGI程序的编写:
这篇文章所用的CGI程序是C语言写的。
先用C语言写一个程序,编译,连接,得到一个.exe的可执行文件。
将这个.exe的可执行文件的扩展名.exe改为.cgi,就好了,这就是一个CGI程序了。
如果不需要一个实例,那么可以看看文章最下面,有简单的代码
以下是C语言代码(以下代码在DevCpp5.11中通过,通过的编译器为GCC 4.9.2 32/64-bit Release,其它编译器大部分也可以正确编译):
forms.c
#include <stdio.h>#include "function.h"void main(){PrintHTTPHead();PrintHTMLHead();PrintHTMLTitle("技微工作室成员注册页面");printf("<style type=\"text/css\">");printf("h1,div{text-align:center;}");printf("div{width:300px;margin-left:auto;margin-right:auto;}");printf("</style>");PrintHTMLBody();printf("<h1>技微工作室成员注册页面</h1>");printf("<div>");printf("<form action=\"cgi/register.cgi\" method=\"post\">");printf("<table border=\"0px\" cellspacing=\"0px\" cellpadding=\"5px\" width=\"300px\">");printf("<tr><td>网名:</td><td><input type=\"text\" name=\"name\"/></td></tr>");printf("<tr><td>密码:</td><td><input type=\"password\" name=\"password\"/></td></tr>");printf("<tr><td>电子邮箱:</td><td><input type=\"text\" name=\"email\"/></td></tr>");printf("<tr><td>备注:</td><td><textarea name=\"memo\" cols=\"20\" rows=\"3\">备注</textarea></td></tr>");printf("<tr><td colspan=\"2\"><input type=\"submit\" value=\"注册\"/><input type=\"reset\" value=\"重置\"/><input type=\"button\" value=\"退出\" onclick=\"javascript:window.close();\"/></td></tr>");printf("</table>");printf("</form>");printf("</div>");PrintHTMLFoot();}
register.c
#include <stdio.h>#include <string.h>#include <stdlib.h>#include "function.h"void main(){PrintHTTPHead();PrintHTMLHead();PrintHTMLTitle("register");PrintHTMLBody();char *method,string[255],*temp,tem[4],ch;int length;FILE *fp;temp=string;method=getenv("REQUEST_METHOD");/*用什么方法提立表单*/if(strcmp(method,"POST")==0){length=atoi(getenv("CONTENT_LENGTH"));if(length>255){printf("您的信息似乎不对!");PrintHTMLFoot();exit(0);}fp=fopen("cgi\\information\\inf.txt","a+");if(fp==NULL){printf("服务器端保存信息失败!");PrintHTMLFoot();exit(0);}fgets(string,length+1,stdin);while(*temp!='\0') {ch=*temp;if(ch=='+'){temp++;continue;}while(ch=='%'){strncpy(tem,temp,3);tem[3]='\0';if(strcmp(tem,"%40")==0)break;temp+=3;ch=*temp;}if(strcmp(tem,"%40")==0){fputc('@',fp);temp+=3;strcpy(tem,"");continue;}if(ch!='&')fputc(ch,fp);elsefputc('\n',fp);temp++;}fputc('\n',fp);fputc('\n',fp);if(fclose(fp)==EOF){printf("文件关闭失败!");PrintHTMLFoot();exit(0);}printf("信息记录成功!");}PrintHTMLFoot();}
function.h
#include <stdio.h>void PrintHTTPHead()/*这个函数必须调用,因为先要发送一个http头*/{printf("Content-type:text/html;");printf("\n\n");/*这后面必须要两个加车,因为http头后面要空一行*/}void PrintHTMLHead(){printf("<html>");printf("<head>");}void PrintHTMLBody(){printf("</head>");printf("<body>");}void PrintHTMLFoot(){printf("</body>");printf("</html>");}void PrintHTMLTitle(char *title){printf("<title>%s</title>",title);}
简单的代码:
#include <stdio.h>void main(){printf("Content-type:text/html;");/*在我教室电脑上测试,可少,不过不推荐不写这个*/printf("\n\n");/*测试,不可少*//*以下就是输出一个html文件的代码,就是这样*/printf("<html>");printf("<head>");printf("<title>WELCOME</title>");printf("</head>");printf("<body>");printf("<h1>WELCOME</h1>");printf("</body>");printf("</html>");}
在这里,用printf函数输出html代码,不用说,大家应该看得出。
如果要提交表单的话,就在form标签的action属性里面填上处理程序。处理程序那边有post和get两种方式获取提交的数据,然后就可以处理了。
CGI程序不只有C语言可以写,不过C语言是我最熟悉的语言,顺手。
没有最好的编程语言,只有最适合自己的。别说Python好,只有适合自己才算好!
CGI程序也可以用Python编写,C++也可以,Perl也可以,不过Perl和Python是脚本。在这,用的是C语言。其实我觉得只要是一个可以编写可执行程序的就可以。VB也行,Delphi也行,C#。
其它关于CGI程序所要用的函数或另外的编程语言实现,大家可以去搜一下。
阅读全文
0 0
- 如何编写CGI程序
- CGI程序编写
- CGI程序编写
- 用c编写cgi程序
- 用c编写cgi程序
- D 语言编写CGI程序
- C语言编写CGI程序
- 编写第一个CGI程序
- 用Delphi编写CGI程序(一)
- 用Delphi编写CGI程序(二)
- 用Delphi编写CGI程序(三)
- 用Delphi编写CGI程序(四)
- 用Delphi编写CGI程序(五)
- 用Delphi编写CGI程序(六)
- 用VB编写标准CGI程序
- 使用Java语言编写CGI程序
- 如何使用C++编写CGI程序接口
- 使用c语言编写cgi程序
- 设置小米5手机拒接中介骚扰标志的电话
- mybatis学习四之输入输出映射以及动态sql
- 178
- 【bzoj3036】绿豆蛙的归宿 期望dp
- 闰年的输入
- CGI程序编写
- SouchTouch JS 报表显示分页、锁定左侧列
- codeforces 427c(强连通分量)
- 第八周项目4-广义表算法库及应用(1)
- 异常行为检测论文学习综述
- H5大番薯牛牛源码下载
- 卷积神经网络实战(可视化部分)——使用keras识别猫咪
- 视频分辨率全表(101种)
- 区别无参、带参、拷贝构造函数、赋值函数