当前位置:   article > 正文

CGI_cgi program

cgi program

CGI

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.
  • 1
  • 2
  • 3
  • 4
  • 5

下面用C语言来表示一个CGI程序

  • 后台示例1为 调用的 CGIC库
  • 后台示例2为 原生代码,只用到了标准输出,获取环境变量.

1/后台示例1

下载地址

完成这个实例有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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

2/后台示例2

只需要一个文件 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

前台

  • form action=”/test” 表示test 被放在了网页根目录下
  • method=”GET” 表示传递方式为GET
<!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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/1019470
推荐阅读
相关标签
  

闽ICP备14008679号