C++ CGI Example Application

The following link is to an interesting website that provides a fairly easy-to-understand tutorial on using C++ in a CGI application.

http://www.geekdaily.net/2007/08/06/c-a-basic-cgi-tutorial/

The Simple CGI ZIP file in this weeks material contains the HTML and C++ source files for an example CGI application. The HTML file can be copied into the directory where your Apache Web Server looks for HTML documents. In a standard Apache installation, the default directory for HTML documents is

C:Program FilesApache Software FoundationApache2.2htdocs

This HTML page presents a simple HTML form for the user to specify his or her first and last name, and a Fahrenheit temperature to be converted to Celsius. This form specifies the HTTP GET method, and the HTTP request is targeted to a CGI script called getEnv.exe.

The three C++ source files are parse.h, parse.cpp, and get_environ.cpp. The parse.h and parse.cpp files provide a simple class that parses HTTP query strings and HTTP cookie strings. The get_environ.cpp is the main CGI application file. All files are heavily commented to explain how the C++ code is interacting with its environment to get HTTP request information. The output from the cout statements is sent to the web server as the HTTP response. To see how this CGI application really works, create an empty Win32 console project on Visual C++ and add these three files to the project. Compile the project and copy the executable file from the Debug directory to the default directory that Apache uses to store CGI executables, which is given below. Make sure that you rename the copy of the executable to getEnv.exe so that it matches the name in the HTML file.

C:Program FilesApache Software FoundationApache2.2cgi-bin

Once you have placed the HTML file and the executable CGI file into the correct directories, start up a browser and enter localhost/SimpleName.html as the URL for the browser to locate. You should see the following: See image.

Enter your first and last names and a Fahrenheit temperature and click the Enter button. You should see something like this. See image.

The comments in the get_environ.cpp file explain what is being returned in the HTTP response. Try executing the HTML page several times. You should see the value of count increase each time. This is because count is reflecting the value of the cookie being shared between the client and the CGI script. You should also use Notepad to edit the HTML file and change the method from get to post. Run the script again to verify that the CGI script receives an HTTP POST request rather than a GET request.

Week 6 Project

Your assignment is to implement a simple number guessing game using HTML forms and a CGI script. The HTML page should introduce the game and provide a form for the user to guess a number. When the form is submitted, the target CGI script should check for cookies containing the number to be guessed and the number of guesses so far. If the cookies dont exist, the script should generate a new number to guess and initialize the guess counter. The script should return an HTML form indicating whether the guess was high, low, or correct. The form should also indicate how many guesses so far, and allow the user to enter another guess. The script should also send the two cookies. When the user has guessed correctly, the script should generate a new value for the number-to-be-guessed cookie and reset the guess counter cookie.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.