×

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

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

SDL library in C++ with Examples

SDL stands for Simple DirectMedia Layer. Using OpenGL and Direct3D, it is a cross-platform development library created to give users low-level access to audio, keyboard, mouse, joystick, and graphics hardware....

3 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

4 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example, The array number 147 is 1,4,7. To add...

3 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

Multilevel Inheritance

C++ Multilevel Inheritance Multilevel inheritance is such an inheritance in which a derived class is created from another derived class. C++ Multilevel Inheritance Example In this example, a base class Student is inherited in...

2 minutes read.

C++ Reading file

In file handling, read() function is used to read data from the file into the program. The read() uses ifstream library to read data from a file. Syntax file-stream-class   file-stream-object;   file-stream-object.read((char *)&var , sizeof (var)); <h3">Example ofstream  outfile;         outfile . read((char*)&emp,sizeof(emp)); C++ File Handling read() Function Example Reading the content of existing...

2 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

C++ Program: Matrix Multiplication

Matrix Multiplication in C++ What is a Matrix? A matrix is a set of numbers in the form of rows and columns forming a rectangular array. It includes numbers, which are often...

4 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 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.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

C++ Range-based For Loop

In C++ language, the range-based for loop was added, which is far superior than the ordinary For loop. The implementation of a range-based for loop doesn't really need much code. It's a...

4 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

swap() function in C++

Swap() function: swap() function in C++: swap() function is a pre-define function in c++ present in STL( Standard template library ). It is used to swap two numbers. It takes two mandatory...

6 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

Initialization of Data Members

In this tutorial, we'll look at how to initialise static member variables in C++. Static members, such as functions or variables, can be added to C++ classes. After declaring the...

1 minute read.