Open In App

Web Programming in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

CGI(COMMON GATEWAY INTERFACE) may be a set of standards that outline however data is changed from the online server, passing the online user’s request to Associate in Nursing application and to receive data back to the user. When any user requests for 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 methodology or convention for passing knowledge back and forth between the server and also the application is termed the common entree interface (CGI) and is an element of the Web’s Hypertext Transfer Protocol (HTTP).

The Common entree Interface (CGI) may be a set of rules for running scripts and programs on an online server. It specifies what data is communicated between the online server and clients’ net browsers and the way the data is transmitted. Most net servers embrace a cgi-bin directory within the root folder of every web site on the server. Any scripts placed during this directory should follow the principles of the Common entree Interface. For example, scripts situated within the cgi-bin directory is also given workable permissions, while files outside the directory may not be allowed to be executed. A CGI script may additionally request CGI surroundings variables, like SERVER_PROTOCOL and REMOTE_HOST, which may be used as input variables for the script.

Since CGI may be a normal interface, it will be used on multiple kinds of hardware platforms and is supported by many varieties net server software package, like Apache and Windows Server. CGI scripts and programs also can be written in many completely different languages, such as C++, Java, and Perl. While several websites still use CGI for running programs and scripts, developers now often include scripts directly within Web pages. These scripts, that area unit written in languages like PHP and ASP, area unit processed on the server before the page is loaded, and also the ensuing knowledge is shipped to the user’s browser.

Browsing the Web
For knowing the thought of CGI, let’s take a glance at the state of affairs that takes place once users browse one thing on the online employing a specific address.

  1. The browser you are using contacts the HTTP web server and demands for the URL.
  2. The web server will parse the URL and will search for the file name; if the requested file is found, immediately sends back that file to the browser or else sends an error message.
  3. The web browser takes the response from a web server and displays either file received or an error message.
  4. If you are developing a website and you required a CGI application to control then you can specify the name of the application in the URL (uniform resource locator) that your code in an HTML file.

Server Side Configuration
Before using the CGI programming, programmers should make sure 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, Apache Web Server is configured to run CGI programs in

/var/www/cgi-bin

Programmers need to have a web server up and running in order to run any CGI program like Perl, shell etc.

Example of CGI program using C++




// C++ example of CGI program
  
#include <iostream>
using namespace std;
int 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";
    return 0;
}


Output:

Content-type:text/html

Hello TutorialsCloud

First CGI program

  • Compile the above program and give this executable a suitable name along with the extension .cgi.
  • This file needs to be kept in
    /var/www/cgi-bin directory

    and it has following content.

  • Some other HTTP headers which are frequently used in CGI programs are:
    1. Content-type: It is a MIME string which defines the format of the file being returned.
    2. Expires: Date: It defines the date the information of the current web page becomes invalid.
    3. Location: URL: The URL that has to be returned instead of the URL that is requested.
    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. This value ‘N’ is used by the browser to report the estimated download time.
    6. Set-Cookie: String: Used to set the cookie passed through the string

CGI Environments Variables

  • CONTENT_LENGTH: Optionally provides the length, in bytes. It’s available only for POST requests.
  • CONTENT_TYPE: Optionally provides the sort of content i.e. the data type of the content.
  • HTTP_COOKIE: come back the visitor’s cookies, if one is ready within the type of key try.
  • HTTP_USER_AGENT: The browser type of the visitor. It request-header field contains info concerning the user agent originating the request.
  • PATH_INFO: It provides the trail for the CGI script.
  • REMOTE_ADDR: The science address of the visitant, i.e. the science address of the remote host creating the request.
  • REMOTE_HOST: The hostname of the visitor, i.e. the totally qualified name of the host creating the request


Last Updated : 14 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads