zoukankan      html  css  js  c++  java
  • 关于linux+apache+c cgi总结

    简单的入门程序

    下面我们来看一个简单的CGI程序,同样也是任何编程的经典入门程序——向屏幕打印“hello world!”

    #include<stdio.h>

    #include<stdlib.h>

    #include<string.h>

    int main(void)

    {

    /*下面这行是必须要的,无论什么程序语言写的CGI程序都必须首先打印content-type:text/html,可以看见前面那个脚本在开始的地方也有一行类似的echo"Content-type:text/plain;charset=iso-8859-1",完了之后必须紧跟一个空行,否则服务器解析不了你的程序,在这儿后面跟了两个'\n'其中第一个'\n'是printf的换行符,第二个'\n'就是我们输出的空行*/

    printf("content-type:text/html;charset=gb2312\n\n");

    printf("hello world!");

    return 0;

    }

    保存为helloworld.c同名编译后在浏览器的执行结果如下:

    查看网页源文件后发现只显示“hello world!”对于我们的printf等C语言元素和我们输出的第一行都是不可见的。现在我们将它稍作一下修改让他以正儿八经的HTML网页显示。

    /*helloworld.c*/

    #include<stdio.h>

    #include<stdlib.h>

    #include<string.h>

    int main(void)

    {

    printf("content-type:text/html;charset=gb2312\n\n");

    printf("<html>\n");

    printf("<head>\n"

    printf("<title>\n");

    printf("hello world!\n");

    printf("</title></head>\n");

    printf("<body>\n");

    printf("<h1>\n");

    printf("<font color=red>\n"); /*HTML中的字体属性*/

    printf("hello world!\n");

    printf("</font>\n");

    printf("<h1></body></html>\n");

    return 0;

    }

    同名编译后执行结果如下:

    网页源文件如下:

    <html><head><title>

    hello world!

    </title></head>

    <body>

    <h1>

    <font color=red>

    hello world!

    </font><h1></body></html>

    可以发现除了第一行外printf中的内容全部输出了,而且HTML标记依然管用。

    表单处理

    其实CGI程序主要是用来处理表单的提交内容的,所以现在开始来学习用CGI处理表单数据先来看一个小例子,HTML页面的输入框中输入输入两个词然后再打印出来。

    HTML(index.htm)程序如下:

    <html>

    <head>

    <title>第一个表单处理程序</title>

    </head>

    <body>

    <form action="../cgi-bin/f_cgi" method="get">

    <!--在我刚开始接触HTML的时候以为action后面的东西的没用的,教材上的实例中action后面接了一个邮件地址,后来我才明白原来action后面是接的表单处理程序--!>

    第一个词<input type="text" name="f" size=10><br>

    第二个词<input type="text" name="s" size=10>

    <input type="submit"value="提交">

    </form>

    </body>

    </html>

    CGI处理程序如下:

    /*f_cgi.c*/

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    int main(void)

    {

           int i=1;

           char *p,*data;

           char tem[4][50];

           printf("content-type:text/html;charset=gb2312\n\n");

    /*从环境变量中读取表单提交的内容,QUERY_STRING为GET请求时用到的环境变量    *里面存放表单的内容*/

           data = getenv("QUERY_STRING");     

           printf("data=%s<br>\n",data);       /*打印表单内容*/

          

    /*将表单提交的内容截断,拿取有用的*/

           p = strtok(data,"=&");    /*strtok函数为字符串截断函数*/

                  strcpy(tem[0],p);

           while((p = strtok(NULL,"=&"))){

                  strcpy(tem[i],p);

                  i++;

           }

           printf("the frist word is %s<br>",tem[1]);/*打印第一个词*/

    printf("the second word is %s<br>",tem[3]);/*打印第二个词*/

    return 0;

    }

    运行后的效果如图:



    POST的话,CGI程序要这样改:

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    int main(void)

    {

           int i=1,len;

           char *p,*data;

           char tem[4][50];

           printf("content-type:text/html;charset=gb2312\n\n");

         /*从环境变量中读取表单提交的内容的字节数这是post得用法*/

           data = getenv("CONTENT_LENGTH");

           len = atoi(data);/*将字节数转为整型*/

           fgets(data,len+1,stdin);/*从标准输入获得表单内容,len+1是为了给表单内容加上'\0'*/

           printf("data=%s<br>\n",data);/*打印表单内容*/

           /*将表单提交的内容截断拿取有用的*/    

           p = strtok(data,"=&");/*strtok函数为字符串截断函数*/

                  strcpy(tem[0],p);

           while((p=strtok(NULL,"=&"))){

                  strcpy(tem[i],p);

                  i++;

           }

           printf("the frist word is %s<br>",tem[1]);/*打印第一个词*/

           printf("the second word is %s<br>",tem[3]);/*打印第二个词*/

           return 0;

    }

    效果图如下,可以看见data的内容是一样的,但在url后面没有了data的内容:

  • 相关阅读:
    WPF多线程问题
    SQL 使用经验
    [转]express 路由控制--next
    [转]浅谈Web缓存
    [转]一份优秀的前端开发工程师简历是怎么样的?
    http
    [转]HTTP详解(1)-工作原理
    [转]使用Flexible实现手淘H5页面的终端适配
    [转]理解$watch ,$apply 和 $digest --- 理解数据绑定过程
    GMT时间
  • 原文地址:https://www.cnblogs.com/sdgwc/p/3123347.html
Copyright © 2011-2022 走看看