Open In App

Web Programming in C++

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

CGI Environments Variables


Article Tags :
C++