Skip to main content

C++ Web Programming

C++ Web Programming

In the earlier chapters of C++, you have learned about the basics of various syntax and way of writing C++ programs for building applications and programs. In this chapter, you will get a new taste of linking web with your C++ program. For this you need to have knowledge of basics of CGI and how it works.

What is CGI?

CGI stands for Common Gateway Interface is a set of standards that defines how information is exchanged from web server, passing the web user’s request to an application program and to receive data back to 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 method or convention for passing data back and forth between the server and the application is called the common gateway interface (CGI) and is part of the Web’s Hypertext Transfer Protocol (HTTP).
The current version is CGI/1.1 and CGI/1.2 is under progress.

Browsing the Web

For knowing the concept of CGI, let us take a look at the scenario that takes place when users browse something on web using a specific URL.
  1. The browser you are using contacts the HTTP web server and demands for the URL.
  2. 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 web server and display either file received or an error message.
If you are developing a web site and you required a CGI application to control then you can specify the name of the application in the URL (uniform resource locator) that you code in an HTML file.

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

Here is an example of 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 suitable name along with the extension .cgi. This file needs to be kept in /var/www/cgi-bin directory and it has following content. Before running your CGI program make sure that you have change mode of file using chmod 755 cplusplus.cgi UNIX command to make file executable. The above C++ program writes its output on STDOUT file i.e. on the screen. There are some other HTTP headers which are frequently used in CGI programs. They 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 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 thestring

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


Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...