linux下的ccgi基础

来源:互联网 发布:淘宝官方加盟可靠吗 编辑:程序博客网 时间:2024/05/17 01:31

        以下代码环境均在ubuntu11。10,web服务器为apache2。如果是apache2的default配置的话,cgi.index要放置在/var/www下面,cgi的bin文件要存放在/uar/lib/cgi-bin文件夹下。这个可以查看/etc/apache2/sites-available/default这个文件。嵌入式系统一般用boa,同理应放置在boa所需规定的cgi文件夹下面

        html的发送请求有两种方式,一种是get,另一种是post。get的话,url会改变,参数能查看到,参数间用&符号隔开,安全性较差且不能传送大量数据。至于post的话,就是将整个表单传送过去,安全性较好,且能传送大量数据。

        下面的代码例子中,用get做加法运算,用post做乘法运算

代码:

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>

get:

#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");printf("data:%s",data);        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

#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);printf("%s\n",poststr);                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;}



原创粉丝点击