×

Web Development in C++

Before learning above C++ web development, we need to learn about CGI

What is CGI?

CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of information happens between the webserver. Exchange of information means how the application takes the user's request and responds to it by sending back the data. This common gateway interface is a part of the Hypertext Transfer Protocol (HTTP).

Now let's take one example and check how the common gateway interface is used.

  • When you open a website, the browser will contact the HTTP server and ask for a URL.
  • Then the HTTP web server will send the URL and search for the file if the user requests a file. If the file is found, the server will send the file to the browser.
  • If the file is not found, then the web server will display an error.
  • The browser will take the response from the server and will display if the file is found or it will display the error

Example: Now let's see an example of the CGI in C++

#include <bits/stdc++.h>
using namespace std;
int main ()
{
  cout << "Content-type:text/html\r\n\r\n";
  cout << "<html>\n";
  cout << "<head>\n";
  cout << "<title>Hello Tutorials and example </title>\n";
  cout << "</head>\n";
  cout << "<body>\n";
  cout << "<h3> <b> First CGI program </b> </h2>\n";
  cout << "</body>\n";
  cout << "</html>\n";
}

We need to compile the program, and then an executable file will be created with the extension .cgi. We need to place this file inside /var/www/CGI-bin. Before running the CGI program, we need to change the mode of the file using the UNIX command so that the file gets executed

chmod 755 filename.CGI

There are different CGI environment variables:

  • CONTENT_LENGTH: The length of the information will be returned.
  • CONTENT_TYPE: Data type of the content to be returned will be printed.
  • HTTP_COOKIE: In the HTTP headers, the set cookie will be returned.
  • HTTP_USER_AGENT: The name of the web browser will be returned.
  • PATH_INFO: The path where the CGI script is stored will be returned.
  • REMOTE-ADDR: The IP address of the requested derive will be returned.
  • REMOTE_HOST:  The name of the host making the request will be printed.
  • SCRIPT_FILENAME: CGI script of the full path will be returned.
  • SCRIPT_NAME: The name of the CGI script will be returned.
  • SERVER_NAME: The IP address of the hostname will be returned.
  • SERVER_SOFTWARE: The version and the name of the server's software will be returned.

Different HTTP heads are frequently used in CGI programs:

  • Content type: This will specify the type of file returned.
  • Expires: Date: This will return, when will the file be expired.
  • Location: URL: This will change the URL to which the URL is requested.
  • Last-modified: Date: The date of the last modification done to the resource is returned.
  • Content-length: The length of the data to be returned will be returned. This is used to print the value of the estimated time for downloading the file.
  • Set-Cookie: String The cookie will be passed in the form of the string.

CGI program that has all the CGI varaibles:

#include <iostream>
#include <stdlib.h>
using namespace std;


const string ENV[ 24 ] = {
   "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
   "HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING",
   "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",
   "HTTP_HOST", "HTTP_USER_AGENT", "PATH",
   "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT",
   "REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME",
   "SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN",
   "SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL",
   "SERVER_SIGNATURE","SERVER_SOFTWARE" };


int main () {
   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>CGI Environment Variables</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";
   cout << "<table border = \"0\" cellspacing = \"2\">";


   for ( int i = 0; i < 24; i++ ) {
      cout << "<tr><td>" << ENV[ i ] << "</td><td>";


      // attempt to retrieve the value of the environment variable
      char *value = getenv( ENV[ i ].c_str() );
      if ( value != 0 ) {
         cout << value;
      } else {
         cout << "Environment variable does not exist.";
      }
      cout << "</td></tr>\n";
   }


   cout << "</table><\n";
   cout << "</body>\n";
   cout << "</html>\n";


   return 0;
}  

Before running the CGI program, we need to change the file mode using the UNIX command so that the file will execute. We need to compile the program, and then an executable file will be created with the extension .cgi. We need to place this file inside /var/www/CGI-bin.


Related Topics

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

Palindrome Using While Loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

How to implement map in C++

Part of the C++ STL is maps (Standard Template Library). Maps are associative containers that hold sorted key-value pairs, where each key is distinct and may only be added or...

4 minutes read.

Depth First Search Program to Traverse a Graph in C++

Depth First Search (DFS) is a technique that is used for transversing the graph. The process of Depth First Search (DFS) starts from the root node and then next comes...

6 minutes read.

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

C++ Bitwise XOR Operator

Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies....

4 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

How to improve programming skills in C++

Before getting started, one should know why to improve their programming skills. To become a good software developer or programmer, one must be skilled in at least one programming language. Many...

4 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

C++ Continue

In C++, the continue statement is a useful tool for avoiding specific scenarios without breaking the loop. It is employed inside loops to move directly to the following iteration and...

4 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.