Cgi form processing

Source: Internet
Author: User

In HTML, when the customer fills out the form and presses the send button, the content of the form is sent to the server, generally, in this case, you need a server script to process the Form Content, save them, query by content, or something else. Without CGI, the web world completely loses its interaction. All information becomes one-way, and no feedback is enough.

Some people think that the CGI program can be replaced by Java Script, which is actually a conceptual error. Javascript can only run in the client browser, while cgi works on the server. Their work involves some intersections, such as form data verification, but Java Script cannot replace CGI. But it can be said that if a job can be done using Java Script and CGI, JavaScript must be used for execution speed, java Script has inherent advantages over CGI. CGI should be used only for issues that cannot be solved on the client, such as interacting with a remote database.

To put it simply, CGI is used to communicate interfaces between HTML forms and server programs ). It is an interface, that is, CGI is not a language, but a set of specifications that can be applied by other languages. In theory, you can use any programming language to compile CGI programs, as long as the programs comply with the CGI specifications defined in some of them. C language performs well in terms of platform independence (almost any system platform has its corresponding compiler) and is quite familiar to most programmers (unlike Perl ), therefore, C is one of the preferred languages for CGI programming. Here we will introduce how to use C to write CGI programs.

The simplest example of CGI programming is form processing. In this article, we will introduce how to use C to write CGI programs for table processing.

Get Form Processing

For forms that use the "method = get" attribute (or do not have the method attribute, get is the default value), CGI defines that when a form is sent to the server for disconnection, the data in the form is stored in an environment variable called QUERY_STRING on the server. The processing of such forms is relatively simple. You only need to read the environment variables. This is different for different languages. In the C language, you can use the library function getenv (defined in the standard library function stdlib) to access the environment variable value as a string. After obtaining the data in a string, you can use some tips to convert data types. This is simple. The standard output in the CGI program (such as the stdout file stream in C) is also redefined. It does not generate any output content on the server, but is redirected to the client browser. In this way, if you write a c cgi program and output an HTML document to its stdout, the HTML document will be displayed in the browser of the client. This is also a basic principle of CGI programs. Let's take a look at the specific program implementation. The following is an HTML form:

<Form action = "/cgi-bin/mult. cgi">

<P> enter the multiplier and the multiplier below. Click OK to see the result.
<Input name = "M" size = "5">
<Input name = "N" size = "5"> <br>
<Input type = "Submit" values = "OK">
</Form>
The function we need to implement is very simple, that is, to multiply the input values in the form and then output the results. In fact, this function can be fully implemented using Java Script, but in order to make the program as simple as possible, I chose this small multiplication as an example.

The following is the CGI program that processes the form, which corresponds to the action attribute value in the form tag.

# Include <stdio. h>
# Include <stdlib. h>
Int main (void)
{
Char * data;
Long M, N;
Printf ("% S % C", "Content-Type: text/html; charset = gb2312", 13, 10 );
Printf ("<title> multiplication result </title> ");
Printf ("Data = getenv ("QUERY_STRING ");
If (Data = NULL)
Printf ("<p> error! Data is not input or there is a problem with data transmission ");
Else if (sscanf (data, "m = % LD & n = % lD", & M, & N )! = 2)
Printf ("<p> error! The input data is invalid. The number must be entered in the form. ");
Else
Printf ("<p> % LD and % ld: % lD. ", M, n, m * n );
Return 0;
}

The specific c syntax will not be discussed much. Let's look at it as a special place for CGI programs.
As mentioned above, the standard output content is to be displayed in the browser. The output content of the first line is required and unique to a CGI program: printf ("% S % C", "Content-Type: text/html", 13, 10). The output is used as the HTML file header. CGI not only outputs HTML text like a browser, but also outputs images, sounds, and other things. This line tells the browser how to handle the received content. There are two blank rows after the definition of Content-Type, which is also indispensable. Because the head output of all CGI programs is similar, you can define a function for it to save programming time. This is a common technique in CGI programming.

The program then calls the library function getevn to obtain the QUERY_STRING content, and then uses the sscanf function to retrieve each parameter value. Note the usage of the sscanf function. Nothing else. It is no different from general C Programs.

After the program is compiled, it is renamed mult. cgi and placed under the/cgi-bin/directory to be called by the form. In this way, a CGI program that processes the get form will be finished.

Post Form Processing
Next we will consider another form transfer method: Post. Assume that the task we want to implement is as follows: add the text entered by the customer in the form to the end of a text file on the server. This can be seen as the prototype of a message board program. Obviously, this job cannot be implemented using client scripts such as Java Script.
The CGI program in the real sense.

It seems that this question is very similar to the content mentioned above, just using different forms and different scripts (programs. But in fact, there are some differences between them. In the above example, the get processing method can be considered as a "Pure query" type, that is, it has nothing to do with the status. The same data can be submitted at any number of times without causing any problems (except for some small overhead of the server ). But the current task is not the same, at least it needs to change the content of a file. Therefore, it can be said that it is related to the status. This is also one of the differences between post and get. In addition, get imposes restrictions on the form length, whereas post does not. This is also the main reason for using the POST method in this task. However, the get processing speed is faster than that of post.

In the definition of CGI, the content of a post-type form is sent to the standard input (stdin in C) of the CGI program ), the transmitted length is placed in the environment variable content_length. Therefore, we need to read the content_length string in the standard input. Reading data from the standard output seems easier than reading data from the environment variables. In fact, it is not. Pay attention to some details, this can be seen in the following program. Note that CGI programs are different from general programs. General programs get an EOF mark after reading the content of a file stream. However, in the form processing process of CGI programs, EOF will never appear, so do not read more characters than content_length. Otherwise, what will happen, no one knows (the CGI specification is not defined. Generally, there are different solutions based on different servers ).

Let's take a look at how to collect data from the post form to the CGI program. Below is a simple C source
Generation:
# Include <stdio. h>
# Include <stdlib. h>
# Define maxlen 80
# Define extra 5
/* Four bytes are reserved for the field name "data", and one byte is reserved for "= "*/
# Define maxinput maxlen + extra + 2
/* Leave one byte to the line break, and another null to the end */
# Define datafile "../data/data.txt"
/* File for which data is to be added */
Void unencode (char * SRC, char * Last, char * DEST)
{
For (; SRC! = Last; SRC ++, DEST ++)
If (* src = "+ ")
* DEST = "";
Else if (* src = "% "){
Int code;
If (sscanf (SRC + 1, "% 2x", & code )! = 1) code = "? ";
* DEST = code;
SRC + = 2 ;}
Else
* DEST = * SRC;
* DEST = "";
* ++ DEST = "";
}

Int main (void)
{
Char * lenstr;
Char input [maxinput], data [maxinput];
Long Len;
Printf ("% S % C ",
"Content-Type: text/html; charset = gb2312", 13, 10 );
Printf ("<title> response </title> ");
Lenstr = getenv ("content_length ");
If (lenstr = NULL | sscanf (lenstr, "% lD", & Len )! = 1 | Len> maxlen)
Printf ("<p> incorrect form submission ");
Else {
File * F;
Fgets (input, Len + 1, stdin );
Unencode (input + extra, input + Len, data );
F = fopen (datafile, "");
If (F = NULL)
Printf ("<p> sorry, unexpected error, cannot save your data ");
Else
Fputs (data, F );
Fclose (f );
Printf ("<p> Thank you very much. Your data has been saved <br> % s", data );
}
Return 0;
}
In essence, the program first obtains the data length from the content_length environment variable, and then reads the corresponding length string. Because the data content is encoded during transmission, it must be decoded accordingly. The encoding rules are very simple, mainly including the following:
1. Each field in the form is represented by a field name followed by an equal sign, followed by the value of this field, and the content between each field is connected;
2. All space characters are replaced by the plus sign, so spaces in the encoding code segment are invalid;
3. Special characters, such as punctuation marks and special characters such as "+", are expressed by the percent sign followed by the corresponding acsii value.
For example, if the user inputs:
Hello there!
Then, when the data is transmitted to the server, it becomes the unencode () function above data = Hello + there % 21, which is used to decode the encoded data. After decoding, the data is added to the end of the Data. txt file and displayed in the browser.

After the file is compiled, change it to collect. cgi and put it in the CGI directory to be called by the form. The corresponding form is given below:

<Form action = "/cgi-bin/collect. cgi" method = "Post">
<P> enter your message (up to 80 characters): <br>
<Input name = "data" size = "60" maxlength = "80"> <br>
<Input type = "Submit" values = "OK">
</Form>

In fact, this program can only be used as an example and cannot be officially used. It misses a key question: when multiple users write data like a file at the same time, there will certainly be errors. For such a program, the probability of simultaneous file writing is very high. Therefore, more considerations need to be made in the more formal messaging program, such as adding a semaphore or using a key file. Because it is just a technical problem of programming, I will not talk about it here.

At last, we will write a CGI program that browses the data.txt file. This only needs to output the content to stdout:
# Include <stdio. h>
# Include <stdlib. h>
# Define datafile "../data/data.txt"
Int main (void)
{
File * f = fopen (datafile, "R ");
Int ch;
If (F = NULL ){
Printf ("% S % C ",
"Content-Type: text/html; charset = gb2312", 13, 10 );
Printf ("<title> error </title> ");
Printf ("<p> <em> Unexpected error, file cannot be opened </em> ");}
Else {
Printf ("% S % C ",
"Content-Type: text/plain", 13, 10 );
While (CH = GETC (F ))! = EOF)
Putchar (CH );
Fclose (f );}
Return 0;
}

The only thing to note in this program is that the parser does not pack data.txt into HTML format before output, but directly serves as a simple text (plain text) output, in this case, you only need to replace text/html with the text/plain type in the output header. the browser automatically selects the corresponding processing method based on the Content-Type type.
It is easy to trigger this program. Because there is no data to input, you only need one button to do it:
<Form action = "/cgi-bin/viewdata. cgi">
<P> <input type = "Submit" values = "">
</Form>

Here, some basic principles of writing CGI programs in C will be completed. Of course, it is difficult to compile a good CGI program based on the content mentioned above. This requires further study of the standardized definition of CGI and some other unique CGI programming skills.

The purpose of this article is to understand the concept of CGI programming. In fact, some mainstream server-side scripting languages, such as ASP, PHP, and JSP, basically have most of the functions of CGI programming, but they are in use, it is indeed much easier than CGI programming in whatever language. Therefore, when programming on the server side, these Script Programming Languages are generally considered first. CGI is used only when they cannot solve the problem, for example, when some underlying programming is required.

 

Cgi form processing

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.