In the earlier tutorials of C++, you have learned the basics of various syntax and ways of writing C++ programs for building applications and programs. This tutorial will teach you how to connect the World Wide Web to your C++ program. For this, you need to know the basics of CGI and how it works.



What is CGI?

CGI, Common Gateway Interface, is a set of standards that define how information is exchanged from the web server, passing the web user's request to an application program and receiving data back to the user. When any user requests a web page, the server sends back the requested page. The Web server typically passes the form information to a small application program that processes the data and may send back a confirmation message. This method or convention for passing data back and forth between the server and the application is called the Common Gateway Interface (CGI). It is part of the Web's Hypertext Transfer Protocol (HTTP).

The current version is CGI/1.1, and CGI/1.2 is in progress.

Browsing the Web

To know the concept of CGI, let us look at the scenario that occurs when users browse something on the web using a specific URL.

  1. The browser you use contacts the HTTP web server and demands the URL.
  2. The web server will parse the URL and search for the file name; if the requested file is found, immediately send back that file to the browser or else send an error message.
  3. The web browser takes the response from a web server and displays either file received or an error message.

If you are developing a website and require a CGI application to control, then you can specify the application's name in the URL (uniform resource locator) that your code in an HTML file.

Server Configuration

Before using CGI programming, programmers should ensure that the Web server supports CGI and is well configured for handling CGI programs. By convention, CGI files will have an extension as .cgi, though they are C++ executable. By default, The Apache Web Server is configured to run CGI programs in /var/www/cgi-bin. Programmers need to have a web server up and running to run any CGI program like Perl, shell, etc.

Here is an example of a CGI program using C++:

Example:

#include <iostream>
void main ()
{
   cout << "Content-type:text/html\r\n\r\n";
    cout << "<html>\n";
     cout << "<head>\n";
     cout << "<title>Hello TutorialsCloud </title>\n";
     cout << "</head>\n";
     cout << "<body>\n";
    cout << "<h3> <b> First CGI program </b> </h2>\n";
    cout << "</body>\n";
   cout << "</html>\n";
}

Compile the above program and give this executable a proper name along with the extension .cgi. This file needs to be kept in /var/www/cgi-bin directory with the following content. Before running your CGI program, make sure you have changed the file mode using the following UNIX command to make the file executable:

chmod 755 filename.cgi

The above C++ program writes its output on the STDOUT file, i.e., on the screen. Some other HTTP headers are frequently used in CGI programs. They are:

  1. Content-type: A MIME string defines the format of the file being returned.
  2. Expires: Date: It defines the date the information on the current web page becomes invalid.
  3. Location: URL: The URL that must be returned instead of the requested URL.
  4. Last-modified: Date: The date of the last modification of the resource.
  5. Content-length: N: The length, in bytes, of the data being returned. The browser uses this value 'N' to report the estimated download time.
  6. Set-Cookie: String: Used to set the cookie passed through the string

CGI Environment Variables

  • CONTENT_LENGTH: Optionally provides the length in bytes. It's available only for POST requests.
  • CONTENT_TYPE: Optionally provides the type of content, i.e., the data type of the content.
  • HTTP_COOKIE: Return the visitor's cookies if one is set in the form of key & value pair.
  • HTTP_USER_AGENT: The browser type of the visitor. The request-header field contains information about the user agent originating the request.
  • PATH_INFO: It gives the path for the CGI script.
  • REMOTE_ADDR: The IP address of the visitor, i.e., the IP address of the remote host making the request.
  • REMOTE_HOST: The hostname of the visitor, i.e., the fully qualified name of the host making the request.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram