C++ Programming Code Examples C++ > Beginners Lab Assignments Code Examples CGI program using C++ Programming CGI program using C++ Programming CGI stands for Common Gateway Interface is a set of standards that define how information is exchanged from the web server, passing the web user's request to an application program 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 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). 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 the file using chmod 755 cplusplus.cgi UNIX command to make the 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: Content-type: It is a MIME string which defines the format of the file being returned. Expires: Date: It defines the date the information of the current web page becomes invalid. Location: URL: The URL that has to be returned instead of the URL that is requested. Last-modified: Date: The date of last modification of the resource. 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. Set-Cookie: String: Used to set the cookie passed through the string 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. #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"; }