Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
When you are working on server, There are 2 directories (page 699 24.a and b)
WHAT IS CGI (COMMON GATEWAY INTERFACE)
CGI stands for Common Gateway Interface and is a simple protocol (standard) to establish a communication (interaction) between your Web page (located anywhere) via a browser and your program that resides on a Web server. The CGI program (e.g. C++, or Perl) processes data submitted by a form and performs requested tasks such as searching. After submitting a form, the browser uses HTTP to make a request for the URL of a CGI program. The Web server receives the request and executes the CGI program with the data that is passed. The output of the CGI program is usually in the form of a web page which is sent back through the Web server to the requesting browser. An example of CGI usage is a database program that runs on the Internet and lets individuals manipulate the database through the web.
Note that a CGI program is executable and resides in a special directory under the direct control of a Webmaster, commonly known as /cgi-bin, so that the Web server is directed to execute the CGI program rather than just display it to the browser.
WRITING A CGI IN C++
A CGI takes information from forms on a Web page, processes it, and sends a page back to the user. A CGI program written in C++ is nothing more than just another C++ program that accepts a string as its input and breaks down the input string into tokens (words) and identifies and processes the input based on the requested task. CGI is language independent and the languages Perl and Python or even a script language such as Unix shell can be used to write a CGI program. One caution, do not try to use Java to write a CGI; instead, use Java Servlets. The language Perl, because of its sophisticated pattern matching (regular expressions), has been the language of choice for writing CGI programs. However, you can stick to C/C++ and write a CGI, rather than using other languages.
In summary, in order to write a simple CGI program in C++, use the input routines of C++ cin or getline( ) to take the data as if it were on a standard input (stdin) with the understanding of how the inputs are separated from each other (e.g. by & or by ; ). After that, a CGI program is another C++ program and when all processing is done, at the end, the CGI program has to communicate to the web page or create a new one. The CGI communicates back to the web page through the standard output (stdout) such as cout with embedded HTML tags inside the quotations. Note that the CGI program takes input from the HTML form and encodes it (URL encoding) by making a single string, since URL does not allow spaces. Instead of spaces, separators (delimiters) such as & (ampersand) and = (equal sign) are used. In the following example, the string with two input data such as the first name and last name are separated with & is sent to the CGI and the program has to strip the f= and save the values correspondingly. For simplicity, we are using one letter for the name of the field.
f=John&f=Doe
C++ CGI PROGRAM
The following HTML file consists of a form with one input data and we want to send the input and get a response by echoing the input back. At first, an HTML file is created and, in the form tag, the action is specified by indicating the name of the executable CGI program (e.g. ebrahimi.cgi) which resides in the /cgi-bin directory. The method of sending the data to the CGI program is POST where the data is sent via the program’s standard input in contrast to the other method GET where the data is sent through a program variable name known as environment variable. For the sake of simplicity, the field’s name is chosen as one character (e.g. f ) which with the = sign makes two characters (e.g. f =). One job of the CGI is to strip off these two characters and save the rest (e.g. value). The following CGI program takes the encoded URL, strips the first two characters, and saves the rest of the string into another string (e.g. str2), which is echoed back to the user.
Note that the "Content-type: text/html\n\n" in
cout<<"Content-type: text/html\n\n";
will inform the server that the CGI program is about to send data to the user in the form of an HTML page. Make sure to include the newline (\n).
If you need to convert the above C++ program to C language for the reason of speed, you only need to change cin to scanf(“%s”, ) and cout to printf( “%s”, ) and make sure to include #include .
COMPILING A CGI PROGRAM
The following compiles a CGI program under Linux (GNU c++ compiler known as g++). However, the executable code is redirected to a file with the extension .cgi. The command chmod (change mode) makes the executable code accessible to the public (read, no write, execute), but you can have all access permissions (read, write, execute).
g++ -o ebrahimi.cgi ebrahimi.cpp
chmod 755 ebrahimi.cgi
Other operating systems may work differently than a Unix-like OS, but the concept would be the same. Note that one reason to place the CGI program in a specific directory like /cgi-bin is for security reasons and to make it easier for the system administrator to manage.
A C++ CGI PROGRAM WITH TWO INPUT DATA
The following C++ CGI program uses two input with the hope that you will understand how to write a multiple input program using the same strategy. The strategy is that every data has a field name followed by =, its value, and a separator (&) before the next field’s name. The program has to skip two characters to get to the value of the first input and, after that, three characters must be skipped between values. For example: f=John&f=Doe
Note that the below program is just for you to understand the concept and you may want to generalize it so it can work with any input and any size field name.
one is 1)”CGI-bin” which are executable and you put your program here. 2)“public_html” you put your web pages here
-Certain information can’t be on the server. What are the information that you want to save on server and nobody sees that? Passwords, credit card information, social security Nos’, employee’s information, and prices.
-Assignment3a: run the program on page 699 as it is and then change it to your own name.
Popular server for widows is II. For Linux machine, the server is Apache both of them are free.
-You can make your computer as a server as you download the IIS (if you have windows) or Apache (if you have Linux) depending on what kind of computer you have
There are
-C++ and Perl is both too much object oriented.
- we use C++ to write the programs because it is faster and all the other languages stick to it. It is the best to handle all the other languages.
- F= is necessary when you are not using a server. when using a server , the server will take care of the f= and we don’t need to use that anymore.
- The program on this page gets the information and just takes it to server and echo the same thing that we gave to it.
- Bin in CGI-bin stands for binary which means executable
- We have 2 strings in this program 1) with “f=”and the other one2) without “f=”
- This line “cout<<"Content-type: text/html\n\n"” in page 699 Figure 20.36b says that I am bring out the html for you.
- Why is called g++? It is a foundation to call these kinds of free service in Linux area which are all excutable.
How to make g++?
g++ -o(o is object) ebrahimi.cgi ebrahimi.cpp
chmod 755(it makes it public) ebrahimi.cgi
-