The Apache server was successfully built in the previous section, which describes the development of CGI programs that can be run on Apache servers using the C language
After installing Apache server, there are some files for developing Apache server under C:\Program files\apache software foundation\apache2.2.
File Description:
Bin:apache Server Software Location
Cgi-bi: Save program written in C language
Conf: saving information for server settings
Error: Handling of Web site sending errors
Htdcocs: Saving HTML programs
Icons: Save icons for developing Apache programs
Include: Save some header files used by the development Web application
Lib: Some of the libraries that are used to develop Web applications are saved
Logs: Log
Manual:apache settings for the server language
Modlules: Save some dynamic link libraries
Program Example 1: develop a CGI version of the Hello World program
Development tools: VS2012 Apache Server
Development steps:
First compile the following code using VS2012
#include <stdio.h>void main () { //Set HTML language printf ("content-type:text/html\n\n"); Print Hello World, I come from a line break in CGI//html for <br> printf ("Hello World, I'm from CGI!<br>");}
After the compilation succeeds in the Debug folder in the project directory there will be an EXE format executable file, I compiled the program generated by the CGI programming. exe
Copy the exe file to the C:\Program files\apache software Foundation\apache2.2\cgi-bin directory, and modify the format of the file to change the CGI programming. EXE to HELLO.CGI
Finally, enter http://localhost/cgi-bin/hello.cgi in IE browser
Program Example 2: traverse all environment variables in Apache server
First compile the following code using VS2012
#include <stdio.h> #include <stdlib.h>void main () { //Set HTML language printf ("content-type:text/html\n \ n "); printf ("eviroments:<br>"); Used to save environment variables extern char** environ; Traverse all environment variables in the CGI for (int i=0;; i++) { if (NULL = = Environ[i]) {break ; } Print all environment variables in CGI printf ("%s<br>", Environ[i]);} }
Copy the exe file to the C:\Program files\apache software Foundation\apache2.2\cgi-bin directory, and modify the format of the file to change the CGI programming. EXE to EVIROMENTS.CGI
Finally, enter in IE browser: http://localhost/cgi-bin/eviroments.cgi
Program Example 3: use environment variables to get the type of server
#include <stdio.h> #include <stdlib.h>void main () { //Set HTML language printf ("content-type:text/html\ N\n "); printf ("%s", getenv ("Server_software"));}
Execution Result:
Program Example 4: use environment variables to output web information
#include <stdio.h> #include <stdlib.h>void main () { //Set HTML language printf ("content-type:text/html\n \ n "); The name, version, and other platform-based additional information for the browser that submitted the form. printf ("%s<br><br>", getenv ("Http_user_agent")); CGI Script runtime host name and IP address printf ("%s<br><br>", getenv ("SERVER_NAME")); The type of your server such as: printf ("%s<br><br>", getenv ("Server_software")); Point to the path to this CGI script, printf ("%s<br><br>", getenv ("Script_name")); Host name of the submission script, this value cannot be set. printf ("%s<br><br>", getenv ("Remote_host")); The host IP address of the submission script. printf ("%s<br><br>", getenv ("REMOTE_ADDR")); Run the CGI version of printf ("%s<br><br>", getenv ("Gateway_interface")); The TCP port that the server is running on. printf ("%s<br><br>", getenv ("Server_port")); The HTTP protocol that the server is running. printf ("%s<br><br>", getenv ("Server_protocol"));}
Execution Result:
Program Example 5: add hyperlinks to Web pages
#include <stdio.h> #include <stdlib.h>void main () { //Set HTML language printf ("content-type:text/html\n \ n "); printf ("<a href= ' http://www.baidu.com ' >www.baidu.com</a>");}
Execution Result:
Program Example 6: use environment variables to implement parameter 1 pass
#include <stdio.h> #include <stdlib.h>void main () { //Set HTML language printf ("content-type:text/html\n \ n "); Char name[256]; Use environment variables to get passed parameters char* queryString = getenv ("query_string"); Parse a string into another string //To print%s in the obtained string to the CNAME sscanf (queryString, "name=%s", name); Prints the passed parameters of printf ("Hello%s<br>", Name);}
Compile the above code using VS2012, after the successful compilation, there will be an EXE file in the Debug folder in the project directory, the EXE file will be modified to name.cgi, and copied to C:\Program Files\apache software In Foundation\apache2.2\cgi-bin
Finally, enter Http://localhost/cgi-bin/name.cgi?name=Tom in the browser
Execution results
Program Example 7: pass multiple parameters using environment variables
#include <stdio.h> #include <stdlib.h>void main () { //Set HTML language printf ("content-type:text/html\n \ n "); int i1,i2;//defines 2 variables for receiving passed arguments //using environment variables to get passed parameters char* queryString = getenv ("query_string"); Parse a string into another string //To print%s in the obtained string to the CNAME sscanf (queryString, "i1=%d&i2=%d", &i1, &i2); printf ("%d+%d=%d", I1, I2, i1+i2);}
Compile the above code using VS2012, after the successful compilation, there will be an EXE file in the Debug folder in the project directory, the EXE file will be modified to sum.cgi, and copied to C:\Program Files\apache software Foundation In \apache2.2\cgi-bin
Finally, enter http://localhost/cgi-bin/sum.cgi?i1=12&i2=13 in the browser
Execution results
Program Example 8: using HTML in CGI
Enter the following HTML language in Notepad and save it in HTML format
Place the saved HTML file in the C:\Program files\apache software Foundation\apache2.2\htdocs directory.
Enter http://localhost/sum.html in the browser
Execution Result:
Calculate the results of a 11+12
Enter 11 and 12 in the input box
The results of the calculation are displayed after you click submit Query content
Development of CGI programs based on Apache server