CGI Scripts
CGI Scripts
Web Browsing
To understand the concept of CGI, lets see what happens when we click a hyper link
available on a web page to browse a particular web page or URL.
Your browser contacts web server using HTTP protocol and demands for the
URL ie. web page filename.
Web Server will check the URL and will look for the filename requested. If web
server finds that file then it sends the file back to the browser without any further
execution otherwise sends an error message indicating that you have requested a
wrong file.
Web browser takes response from web server and displays either the received file
content or an error message in case file is not found.
However, it is possible to set up HTTP server in such a way so that whenever a file in a
certain directory is requested that file is not sent back; instead it is executed as a program,
and whatever that program outputs as a result, that is sent back for your browser to
display. This can be done by using a special functionality available in the web server and
it is called Common Gateway Interface or CGI and such programs which are executed
by the server to produce final result, are called CGI scripts. These CGI programs can be a
PERL Script, Shell Script, C or C++ program etc.
CGI Architecture Diagram
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print '<html>';
print '<head>';
print '<title>Hello Word - First CGI Program</title>';
print '</head>';
print '<body>';
print '<h2>Hello Word! This is my first CGI program</h2>';
print '</body>';
print '</html>';
1;
Now if you click hello.cgi link then request goes to web server who search for hello.cgi
in /cgi-bin directory, execute it and whatever result got generated, web server sends that
result back to the web browser, which is as follows:
This hello.cgi script is a simple Perl script which is writing its output on STDOUT file ie.
screen. There is one important and extra feature available which is first line to be printed
Content-type:text/html\r\n\r\n. This line is sent back to the browser and specifies the
content type to be displayed on the browser screen. Now you must have undertood basic
concept of CGI and you can write many complicated CGI programs using Perl. This
script can interact with any other exertnal system also to exchange information such as a
database, web services, or any other complex interfaces.
For Example:
Content-type:text/html\r\n\r\n
There are few other important HTTP headers which you will use frequently in your CGI
Programming.
Header Description
A MIME string defining the format of the content being
Content-type: String
returned. Example is Content-type:text/html
The date when the information becomes invalid. This should be
used by the browser to decide when a page needs to be refreshed.
Expires: Date String
A valid date string should be in the format 01 Jan 1998 12:00:00
GMT.
The URL that should be returned instead of the URL requested.
Location: URL String
You can use this filed to redirect a request to any other location.
Last-modified: String The date of last modification of the file.
The length, in bytes, of the data being returned. The browser uses
Content-length: String
this value to report the estimated download time for a file.
Set-Cookie: String Set the cookie passed through the string
CGI Environment Variables
All the CGI program will have access to the following environment variables. These
variables play an important role while writing any CGI program.
Here is small CGI program to list down all the CGI variables supported by your Web
server. Click this link to see the result Get Environment
#!/usr/bin/perl
1;
This HTTP header will be different from the header mentioned in previous section. For
example, if you want to make a FileName file downloadable from a given link then it's
syntax will be as follows.
#!/usr/bin/perl
# HTTP Header
print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
http://www.test.com/cgi-bin/hello.cgi?key1=value1&key2=value2
The GET method is the defualt method to pass information from a browser to the web
server and it produces a long string that appears in your browser's Location:box. You
should never use GET method if you have password or other sensitive information to
pass to the server. The GET method has size limitation: only 1024 characters can be
passed in a request string.
You can pass information by simply concatenating key and value pairs alongwith any
URL or you can use HTML <FORM> tags to pass information using GET method.
Simple URL Example : Get Method
Here is a simple URL which will pass two values to hello_get.cgi program using GET
method.
http://www.tutorialspoint.com/cgi-bin/hello_get.cgi?first_name=ZARA&last_name=ALI
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "</head>";
print "<body>";
print "<h2>Hello $first_name $last_name - Second CGI Program</h2>";
print "</body>";
print "</html>";
1;
Here is the actual output of the above form coding. Now you can enter First and Last
Name and then click submit button to see the result.
First Name:
Last Name:
Below is modified hello_post.cgi script to handle input given by web browser. This
script will handle GET as well as POST method.
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "</head>";
print "<body>";
print "<h2>Hello $first_name $last_name - Second CGI Program</h2>";
print "</body>";
print "</html>";
1;
Let us take again same examle as above, which passes two values using HTML FORM
and submit button. We are going to use CGI script hello_post.cgi to handle this imput.
Here is the actual output of the above form coding, You enter First and Last Name and
then click submit button to see the result.
First Name:
Last Name:
Maths Physics
Below is checkbox.cgi script to handle input given by web browser for radio button.
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Checkbox - Third CGI Program</title>";
print "</head>";
print "<body>";
print "<h2> CheckBox Maths is : $maths_flag</h2>";
print "<h2> CheckBox Physics is : $physics_flag</h2>";
print "</body>";
print "</html>";
1;
Below is radiobutton.cgi script to handle input given by web browser for radio button.
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Radio - Fourth CGI Program</title>";
print "</head>";
print "<body>";
print "<h2> Selected Subject is $subject</h2>";
print "</body>";
print "</html>";
1;
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Text Area - Fifth CGI Program</title>";
print "</head>";
print "<body>";
print "<h2> Entered Text Content is $text_content</h2>";
print "</body>";
print "</html>";
1;
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Dropdown Box - Sixth CGI Program</title>";
print "</head>";
print "<body>";
print "<h2> Selected Subject is $subject</h2>";
print "</body>";
print "</html>";
1;
In many situations, using cookies is the most efficient method of remembering and
tracking preferences, purchases, commissions, and other information required for better
visitor experience or site statistics.
How It Works
Your server sends some data to the visitor's browser in the form of a cookie. The browser
may accept the cookie. If it does, it is stored as a plain text record on the visitor's hard
drive. Now, when the visitor arrives at another page on your site, the cookie is available
for retrieval. Once retrieved, your server knows/remembers what was stored.
Expires : The date the cookie will expire. If this is blank, the cookie will expire
when the visitor quits the browser.
Domain : The domain name of your site.
Path : The path to the directory or web page that set the cookie. This may be
blank if you want to retrieve the cookie from any directory or page.
Secure : If this field contains the word "secure" then the cookie may only be
retrieved with a secure server. If this field is blank, no such restriction exists.
Name=Value : Cookies are set and retrviewed in the form of key and value pairs.
Setting up Cookies
This is very easy to send cookies to browser. These cookies will be sent along with HTTP
Header. Assuming you want to set UserID and Password as cookies. So it will be done as
follows
#!/usr/bin/perl
print "Set-Cookie:UserID=XYZ;\n";
print "Set-Cookie:Password=XYZ123;\n";
print "Set-Cookie:Expires=Tuesday, 31-Dec-2007 23:12:40 GMT";\n";
print "Set-Cookie:Domain=www.tutorialspoint.com;\n";
print "Set-Cookie:Path=/perl;\n";
print "Content-type:text/html\r\n\r\n";
...........Rest of the HTML Content goes here....
Here we used Set-Cookie HTTP header to set cookies. It is optional to set cookies
attributes like Expires, Domain, and Path. It is important to note that cookies are set
before sending magic line "Content-type:text/html\r\n\r\n.
Retrieving Cookies
This is very easy to retrieve all the set cookies. Cookies are stored in CGI environment
variable HTTP_COOKIE and they will have following form.
key1=value1;key2=value2;key3=value3....
This will produce following result, provided above cookies have been set before calling
retrieval cookies script.
User ID = XYZ
Password = XYZ123