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);
}