The first CGI program-----is a plain C language. ' (*∩_∩*)

Source: Internet
Author: User

The first CGI program ———— is a plain C language.' (*∩_∩*)Painterq May 14, 2017

In a previous blog post describing the installation and configuration of Apache, I had my first HTTP server in a daze. Although it is the site under the LAN but for the students to drive or completely enough (cough). It seems to have Apache and then drop the HTTP document into the Htdocs folder, and the old driver's job is so easy. In fact, there are many poses have not mastered oh, not the pursuit of new posture of life and salted fish what is the difference! Is the old driver also to do the most posture of the one. Let's take a look at what a Dynamic Web page is and what a CGI is.

1. Dynamic Web pages? So what is CGI?

As the site grew larger, people were finally as lazy as ever. That day, people finally tired of making similar HTML documents over and over again, complaining that the two documents are only a little different to rewrite a point, they began to feel the fear of HTML control, the air of the wisp overflow is the breath of despair. A handsome teenager looked at the powerless people and said, "Why not write a program or script to produce HTML?" ”。 Only one sentence, the thick fog burst, the sun and the moon restored the former brilliance.

A Web page that is dynamically generated based on a user's request is a Dynamic Web page (rather than a Web page that uses JavaScript for dynamic interaction). There is now a shadow of this method everywhere, and the pages that end up with. asp,. jsp,. PHP,. CGI, and so on, are dynamically generated (and no such endings are not meant to be). When the user requests to the server, the server calls the external program running on the server according to the parameters of the request to generate the page content (you can imagine that the program is a generic program or script, he may just be processing the content of the request, he may want to access the database, he can do what he wants to do). CGI is an interface that explains how external programs are called by the server. Specifically, how the parameters are passed in, and how the processed information is returned. Generally said "CGI program" refers to the *.CGI program written in C/s + + (that is, the compiled *.exe directly to the suffix *.cgi. In fact I don't care what language you are, just compile it as an. exe. But strictly speaking CGI does not refer to which language is written in the program or script, but in any language that satisfies the CGI interface written by the program or script are called CGI programs, can be php,python and so on.

2. Environment variables and standard input and output

One Web request at the HTTP level is three parts: the request line, the message header, the request body (the next blog post discusses the HTTP protocol, welcome). Some information about the request line and message header is passed to the CGI program through the environment variable , and the request body is passed to the CGI program in the standard input , and finally, the output message is output in standard mode. and send it to the customer using the HTTP protocol (without output, of course). Standard input and output everyone must be not unfamiliar, is printf (), Cin,cout. Environment variables are "global variables" at the operating system level, and any program can register new environment variables or access existing environment variables, as such environment variables are a way of interacting between processes.

In stdlib.h, the library function getenv () is used to get the environment variable based on the name of the environment variable (char*), which is prototyped as follows

char * getenv (const char *name);

For example, all of Java knows that to set an environment variable named path, we use this function to get the contents of the path, just as a demonstration of the function.

1 #include <cstdlib>2 #include <iostream>3int  main ()4  {5     std::cout << getenv ("path") <<std:: Endl; 6     return 0 ; 7 }

Figure 1: Running results

If there is a problem with the compilation of this program, see: Resolving this function or variable could be unsafe

3. First CGI program

What are the environment variables that CGI needs to use, and we write the first CGI program, which functions to return all the environment variables that need to be used. Many of these environment variables are HTTP protocol headers, welcome to HTTP protocol parsing.

First, you define an array of structures to facilitate a single output.

 for(inti =0; I < +; i++) {Std::cout<<"<li style = \ "Color:blue\" >"; _dupenv_s (&env, &length, nevname[i].name.c_str ()); Std::cout<< Nevname[i].name <<':'; if(env = =nullptr) {Std::cout<<"<br/>"; }Else{std::cout<<"<span style = \ "Color:green\" >"<<env
Std::cout <<"</span>"<<"<br/>"; }
Use a loop to output the contents of each item in turn

Figure 2: Defining an array of structures

After compiling the EXE file into the Apache Cgi-bin folder, and then write an HTML to request the program. And put the files in the Htdocs folder.

Execution Result:

    

Figure 3: Before clicking the button

Figure 4: After clicking submit

The first CGI program-----is a plain C language. ' (*∩_∩*)

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.