CGI programming learning ---- query data of data centers,
0x01: What is CGI programming?
CGI: Common Gateway Interface
CGI stands for the Common Gateway Interface, which makes it possible to run some applications (or gateways) outside the network server.
The CGI-BIN directory is where the CGI script is stored.
These scripts enable the WWW server and browser to run external programs without starting another program.
It is a program running on a Web server and triggered by a visitor. CGI is an interface for running external programs (or gateways) on the HTTP server. It allows network users to access the application type programs on the remote system, it's like they are actually using those remote computers. CGI allows viewers to interact with the server. If you have ever entered a table or searched on the network, it is very likely that CGI is used.
0x02: In what languages can I write CGI programming?
CGI applications can be written in most programming languages, such as Perl (Practical Extraction and Report Language), C \ C ++, Java, and Visual Basic. However, for web page producers who do not have much programming experience, it is a great challenge (html test files are required to test CGI programs ).
0x03: How CGI applications work
1. a browser request through an HTML form or hyperlink refers to the URL of the previous CGI application.
2. the server sends and receives requests.
3. The server executes the specified CGI application.
4. The operations required for CGI application execution are usually based on the content of the visitor.
5. The CGI application formats the result as a document that can be understood by the Web server and the browser (usually an HTML webpage ).
6. The network server returns the result to the browser.
0x04: CGI program implementation
C language implementation code:
1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <string. h> 4 5 char from_hex (char ch) 6 {7 return isdigit (ch )? Ch-'0': tolower (ch)-'A' + 10; 8} 9 10 char * gb2312_to_chinese (char * gb2312) // convert gb2312 encoding to 11 {12 if (strstr (gb2312, "%") 13 {14 char * buf = (char *) malloc (strlen (gb2312) + 1); 15 char * pstr = gb2312; 16 char * pbuf = buf; 17 while (* pstr) 18 {19 if (* pstr = '% ') 20 {21 if (pstr [1] & pstr [2]) 22 {23 * pbuf ++ = from_hex (pstr [1]) <4 | from_hex (pstr [2]); 24 pstr + = 2; 25} 26} 27 else if (* pstr = '+ ') 28 {29 * pbuf ++ = ''; 30} 31 else32 {33 * pbuf ++ = * pstr; 34} 35 pstr ++; 36} 37 * pbuf = '\ 0'; 38 return buf; 39} 40} 41 42 void main () 43 {44 printf ("Content-type: text/html \ n "); // The HTML language 45 char searchstr [256] =" "; 46 gets (searchstr ); // obtain the input 47 char * p = strchr (searchstr, '&') from the browser; // process the input obtained from the browser, remove the additional searchstr [256] = "our input & % ds % ads % fadsa8d8hd" 48 if (p! = NULL) 49 {50 * p = '\ 0'; 51} 52 char * pstart = searchstr + 10; 53 pstart = gb2312_to_chinese (pstart ); // when the input obtained from the browser changes the encoding method of 312, it must be converted to Chinese characters to search for 54 int j = 0; 55 char pathr [200] = "kaifang.txt "; 56 FILE * pfr = fopen (pathr, "r"); // open the data in the open room 57 if (pfr = NULL) 58 {59 puts ("failed to open "); 60 return; 61} 62 else63 {64 while (! Feof (pfr) 65 {66 char buffer [256] = {0}; 67 fgets (buffer, 256, pfr); 68 char * p = strstr (buffer, pstart ); 69 if (p! = NULL) 70 {71 j ++; // counter 72 puts (buffer); 73 puts ("<br> "); // wrap 74} 75} 76 puts ("<br>"); // wrap 77 printf ("% d % s", j, pstart); 78 puts ("<br>"); // wrap 79 puts ("<br>"); // wrap 80} 81}
Html files used to test CGI programs:
1 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 2
0x05: Test Results
Environment: Apache2.2, WIN8.1
Find the installation directory of apacheand generate the written cgiprogram as the. exe executable program. Change the suffix to. cgi and place it in the directory of Apache 2.2 \ cgi-bin.
Place the html file used to test the CGI program in the directory of Apache 2.2 \ htdocs
Effect:
0x06: Note
Printf ("Content-type: text/html \ n"); // HTML Language
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 that the subsequent 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.