CGI programming with C language technology

Source: Internet
Author: User
Tags command line html form

First, CGI overview

The CGI (Public gateway Interface) sets the interface protocol standard for Web servers to invoke other executable programs (CGI programs). The Web server interacts with the Web browser by invoking the CGI program implementation

, that is, the CGI program accepts the information that the Web browser sends to the Web server, processes it, and sends the response back to the Web server and Web browser. The CGI program generally completes the processing of form data, database query and integration with traditional application system in Web pages. CGI programs can be written in any programming language, such as the Shell scripting language, Perl, Fortran, Pascal, C, and so on. But the CGI program written in C language has the characteristics of fast execution, high security (because C language program is compiled and can not be modified).

The CGI interface standard includes standard input, environment variable and standard output.

1. Standard input

CGI programs, like other executable programs, can get input from the Web server via standard input (stdin), such as data in form, which is called the Post method of passing data to a CGI program. This means executing CGI programs on the operating system command line State and debugging CGI programs. Post method is a common method, this article will take this method as an example, analysis of CGI program design methods, processes and techniques.

2. Environment variables

The operating system provides many environment variables that define the execution environment of the program, which the application can access. The Web server and the CGI interface also set up some of their own environment variables to pass some important parameters to the CGI program. The CGI Get method also passes the data in the form to the CGI program through the environment variable query-string.

3. Standard output

The CGI program transmits output information to the Web server through standard output (stdout). The information that is sent to the Web server can be in a variety of formats, usually in plain text or HTML text, so that we can debug the CGI program in the command line state and get their output.

Here is a simple CGI program that prints the information in HTML form directly to a Web browser.

#include
#include
main()
{
int,i,n;
printf ("Contenttype:text/plain\n\n");
n=0;
if(getenv("CONTENT-LENGTH"))
n=atoi(getenv(CONTENT-LENGTH"));
for (i=0;i
putchar(getchar());
putchar ('\n');
fflush(stdout);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.