Brief Guide to write CGI program

xiaoxiao2021-03-06  73

Abstract: CGI specifies the interface protocol standard for the web server to call other executable programs (CGI program). The web server implements the interaction of the CGI program and the interaction of the web browser. The CGI program can be written in any programming language, such as the shell scripting language, Perl, Fortran, Pascal, C language, etc. However, the CGI program written in C is has the characteristics of fast execution speed and high security. This article analyzes the methods, processes, and techniques of CGI program design with C language. Text: CGI program design with CGI Summary, CGI Overview CGI (CommON Gateway Interface: Public Gateway Interface) specifies the interface protocol standard for the web server calling other executable program (CGI program). The web server implements the interaction of the CGI program and the interaction of the web browser, that is, the CGI program accepted the web browser sent to the web server, processes the response results to the web server and web browser. CGI program generally completes processing, database query, and integration of traditional application systems. The CGI program can be written in any programming language, such as the shell scripting language, Perl, Fortran, Pascal, C language, etc. However, the CGI program written in C language has features that perform fast, high security (because C language programs are compiled and cannot be modified). CGI interface standards include standard input, environment variables, standard output three parts. 1. Standard input CGI program Like other executable programs, you can get input information from the web server through standard input (stdin), such as data in the form, which is the so-called POST method to pass data to the CGI program. This means that the CGI program can be executed in the operating system command line status and debug the CGI program. The POST method is a common method. This article will take this method as an example to analyze the methods, processes, and techniques of the CGI program design. 2. The environment variable operating system provides many environment variables that define the execution environment of the program, and the application can access them. Web servers and CGI interfaces have also set up some of their environment variables to deliver some important parameters to the CGI program. The CGI's GET method also passes the data in Form to the CGI program via environment variable Query-String. 3. The standard output CGI program transmits the output information to the web server via the standard output (stdout). Information transmitted to the web server can be used in various formats, usually in the form of plain text or HTML text so that we can debug the CGI program in the command line status and get their output. Below is a simple CGI program that outputs the information in HTML directly to the WE B browser. # include # include main () {INT I, N; Printf ("Content Type: Text / PLAIN / N / N); N = 0; IF (GetENV (" Content- Length ")) N = ATOI (GetENV (Content-length"))); for (i = 0; i

Note the web server does not terminate its output with file endors, so if you do not check the environment variable Content-Length, the CGI program cannot know when the input is over. For (i = 0; i

Note that the value of the last variable of the input data is not to end. Once the name / value is decomposed, some special characters in the input must be converted into the corresponding ASCII characters. These special characters are: : convert to spaces;% xx: Special characters represented by their hexadecimal ASCII code value. Convert it into a corresponding ASCII character based on value xx. This conversion is performed on the Form variable name and variable value. Below is a CGI program that analyzes Form data and delivers the result to the web server. #include #include #include int htoi (char *); main () {Int i, n; char C; printf ("ContentType: Text / PLAIN / N / N "); n = 0; if (" Content-length ")) N = ATOI (" Content-length ")); for (i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-92568.html

New Post(0)