CGI programming-Get and post examples

Source: Internet
Author: User
CGI programming-Get and post examples

I am still new to CGI programming! But as long as you understand C, C-based CGI programming will not be very difficult!
The following describes the application of the get and post methods, and provides a small demo to beginners who have just learned CGI programming!

Get method: to perform an addition operation, you need to receive two parameters.

The file get. C is as follows:
-------------------------------
# Include <stdio. h>
# Include <stdlib. h>
Int main (void)
{
Char * data;
Char A [10], B [10];
Printf ("Content-Type: text/html/n ");
Printf ("<HTML>/N ");
Printf ("Printf ("<body>/N ");
Printf ("<Div style =/" font-size: 12px/">/N ");
Data = getenv ("QUERY_STRING ");
If (sscanf (data, "A = % [^ &] & B = % s", a, B )! = 2 ){
Printf ("<Div style =/" color: red/"> Error Parameters shoshould be entered! </Div>/N ");
}
Else {
Printf ("<Div style =/" color: green;
Font-size: 15px; font-weight: bold/"> A + B =
% D </div>/N ", atoi (A) + atoi (B ));
}
Printf ("<HR color =/" blue/"align =/" Left/"width =/" 100/"> ");
Printf ("<input type =/" button/"value =/" Back cgi/"onclick =/" javascript: window. location = '.. /cgi.html '/"> ");
Printf ("</div>/N ");
Printf ("</body>/N ");
Printf ("Return 0;
}

POST method: to perform a multiplication operation, you need to receive two parameters.

The file post. C is as follows:
--------------------------------
# Include <stdio. h>
# Include <stdlib. h>
Int main (void ){
Int Len;
Char * lenstr, poststr [20];
Char M [10], n [10];
Printf ("Content-Type: text/html/n ");
Printf ("<HTML>/N ");
Printf ("Printf ("<body>/N ");
Printf ("<Div style =/" font-size: 12px/">/N ");
Lenstr = getenv ("content_length ");
If (lenstr = NULL)
Printf ("<Div style =/" color: red/"> Error Parameters shoshould be entered! </Div>/N ");
Else {
Len = atoi (lenstr );
Fgets (poststr, Len + 1, stdin );
If (sscanf (poststr, "m = % [^ &] & n = % s", m, n )! = 2 ){
Printf ("<Div style =/" color: red/"> error: parameters are not right! </Div>/N ");
}
Else {
Printf ("<Div style =/" color: green;
Font-size: 15px; font-weight: bold/"> M * n =
% D </div>/N ", atoi (m) * atoi (n ));
}
}
Printf ("<HR color =/" blue/"align =/" Left/"width =/" 100/"> ");
Printf ("<input type =/" button/"value =/" Back cgi/"onclick =/" javascript: window. location = '.. /cgi.html '/"> ");
Printf ("</div>/N ");
Printf ("</body>/N ");
Printf ("Fflush (stdout );
Return 0;
}

Add the HTML test file cgi.html:
--------------------------------
<HTML>
<Head>
<Title> CGI testing </title>
</Head>
<Body>
<Table width = "200" Height = "180" border = "0" style = "font-size: 12px">
<Tr> <TD>
<Div style = "font-weight: bold; font-size: 15px"> method: Get </div>
<Div> Please input two number: <div>
<Form method = "get" Action = "./cgi-bin/get">
<Input type = "TXT" size = "3" name = "A"> +
<Input type = "TXT" size = "3" name = "B"> =
<Input type = "Submit" value = "sum">
</Form>
</TD> </tr>
<Tr> <TD>
<Div style = "font-weight: bold; font-size: 15px"> method: Post </div>
<Div> Please input two number: <div>
<Form method = "Post" Action = "./cgi-bin/Post">
<Input type = "TXT" size = "3" name = "M"> *
<Input type = "TXT" size = "3" name = "N"> =
<Input type = "Submit" value = "resu">
</Form>
</TD> </tr>
<Tr> <TD> <inputtype = "button" value = "back
Home "onclick = 'javascript: window. Location ="./index.html "'> </TD> </tr>
</Table>
</Body>
</Html>

Brief descriptions:


(1) printf ("Content-Type: text/html/n ");
This line uses the standard output to send the string "contenttype: text/plain/n" to the web server. It is a MIME header that tells the Web Server
The output is in plain ASCII text format. Note that there are two line breaks in this header, because the Web server needs to see a blank line before the actual text information starts.

(2) Data = getenv ("QUERY_STRING ");
CGI definition: When a form submitted by the get method is sent to the server for disconnection, the data in the form is saved 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.

(3) sscanf (data, "A = % [^ &] & B = % s", a, B )! = 2
This is about the use of the sscanf function. You can search for it online!

(4) atoi (A) + atoi (B)
The function of the atoi function is to convert the struct type into an integer. addition operations can be performed only after conversion!

(5) lenstr = getenv ("content_length ");
The Web server sets this environment variable when calling a CGI program using the POST method. Its text value indicates the number of characters in the input sent by the Web server to the CGI program. Therefore, the atoi () function must be used () convert the value of this environment variable to an integer and assign it to the variable Len (defined below ).

(6) fgets (poststr, Len + 1, stdin );
This is about the use of the fgets function. You can search for it online!

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.