home PYTHONJAVA
 

Perl CGI Programming


What is CGI

CGI is currently maintained by NCSA, and NCSA defines CGI as follows:

CGI (Common Gateway Interface), a common gateway interface, which is a program that runs on a server such as an HTTP server and provides an interface to the client's HTML page.


Web browsing

To better understand how CGI works, we can click on a link or URL from the web page:

  • 1. Use your browser to access the URL and connect to the HTTP web server.
  • 2. After receiving the request information, the web server will parse the URL and find out whether the accessed file exists on the server. If there is content of the returned file, an error message is returned.
  • 3. The browser receives information from the server and displays the received file or error message.

CGI programs can be Python scripts, PERL scripts, SHELL scripts, C or C++ programs, and more.


Web Server Support and Configuration

Before you do CGI programming, make sure your web server supports CGI and CGI handlers.

Apache supports CGI configuration:

Set up the CGI directory:

ScriptAlias /cgi-bin/ var/www/cgi-bin/

All HTTP server execution CGI programs are stored in a pre-configured directory. This directory is called the CGI directory and, by convention, it is named the /var/www/cgi-bin directory.

CGI files have a .cgi extension, and Perl can also use the .pl extension.

By default, the Linux server configuration runs in the cgi-bin directory as /var/www.

If you want to specify a different directory to run CGI scripts, you can modify the httpd.conf configuration file as follows:

<Directory "/ Var/www/cgi-bin">
   AllowOverride None
   Options +ExecCGI
   Order allow,deny
   Allow from all
</Directory>

Add the .pl suffix to the AddHandler so that we can access the Perl script file ending in .pl:

AddHandler cgi-script .cgi . pl .py

First CGI application

Here we make an investigate at.Cgi archive with the accompanying code:

test.Cgi code

#!/usr/bin/perl print "Content-kind:textual content/htmlrnrn"; print '<html>'; print '<head>'; print '<meta charset="utf-8">'; print '<identify> progaramming academic (welookups.Com)</name>'; print '</head>'; print '<frame>'; print '<h2>Hello Word! </h2>'; print '<p>The first CGI software from the progaramming academic. </p>'; print '</frame>'; print '</html>'; 1;
The output of the first line of the script "Content-kind:textual content/htmlrnrn" is sent to the browser and tells the browser that the content type displayed is "text/html".



HTTP header

The "Content-type:text/html" in the contents of the test.cgi file is part of the HTTP header and is sent to the browser to tell the browser the content type of the file.

The format of the HTTP header is as follows:

HTTP Field Name: /span>Field Content

For example:

Content-type:text/html\r\n\r\n

The following table describes the information commonly used by HTTP headers in CGI programs:

HeadDescription
Content-type: Requested MIME information corresponding to the entity. For example: Content-type:text/html
Expires: Date Time and time when the response expired
Location: URL is used to redirect the recipient to the location of the unsolicited URL to complete the request or identity New resources
Last-modified: DateReserved resource last modified time
Content-length: NRequested content length
Set-Cookie: String Set Http Cookie

CGI condition variable

All CGI programs procure the accompanying condition factors, which play an indispensable position in CGI programs:

variable namedescription
CONTENT_TYPEThe cost of this condition variable proposes the MIME kind of the records gave. As of now, nature variable CONTENT_TYPE is normally: utility/x-www-shape-urlencoded, which implies that that the data originates from a HTML shape.
CONTENT_LENGTHIf the server and CGI program information is surpassed in POST, this surroundings variable can think about the assortment of bytes of genuine insights even from the standard info STDIN. This surroundings variable should be utilized when concentrating the entered certainties.
HTTP_COOKIECOOKIE content inside the client.
HTTP_USER_AGENT Provides purchaser program records that comprises of the amount of varieties or other exclusive data.
PATH_INFOThe cost of this condition variable speaks to the next course records straight away after the CGI program call. It frequently appears as a parameter to CGI bundles.
QUERY_STRINGIf the server and CGI programming data is outperformed in GET, the cost of this surroundings variable is despite the fact that the records is passed. This actualities pursues the name of the CGI program, isolated by methods for a question mark '?'.
REMOTE_ADDRThe cost of this condition variable is the IP manage of the buyer that despatched the solicitation, comprising of 192.168.1.Sixty seven above. This cost continually exists. What's more, it's far the interesting identifier that the Web supporter wishes to offer to the Web server, which might be used in CGI projects to separate between various Web customers.
REMOTE_HOST The expense of this condition variable contains the hostname of the customer that despatched the CGI demand. On the off chance that you do never again help the question you need, you do never again need to diagram this surroundings variable.
REQUEST_METHOD Provides the methodology through which the content is called. For contents that utilization the HTTP/1.0 convention, best GET and POST bode well.
SCRIPT_FILENAMEFull way to CGI script
SCRIPT_NAMEThe call of the CGI script
SERVER_NAMEThis is the host name, assumed name or IP manage of your web server.
SERVER_SOFTWAREThe cost of this surroundings variable conveys the call and model scope of the HTTP server that known as the CGI program. For example, the expense above is Apache/2.2.14 (Unix)

The following is a simple CGI content that yields CGI surroundings variables:

Instance

#!/usr/bin/perl print "Content-kind: textual content/htmlnn"; print '<meta charset="utf-eight">'; print "<font length=+1>Environment variables: </font>n"; foreach (type keys %ENV) print "<b>$_</b>: $ENV$_<br>n"; 1;

File download

If we need to put in force file down load thru Perl CGI, we need to set one-of-a-kind header facts as follows:

Instance

#!/usr/bin/perl # HTTP Header print "Content-Type:utility/octet-flow; call=" FileName"rn"; print "Content-Disposition: attachment; filename="FileName"rnn"; # Actual File Content will move hear. open( FILE, "<FileName" ) ; whilst(examine(FILE, $buffer, 100 ) ) print("$buffer");

Transfer information the use of the GET technique

The GET method sends the encoded consumer facts to the server. The records statistics is covered in the URL of the request page, separated with the aid of a "?", as follows:

http://www .Check.Com/cgi-bin/take a look at.Cgi?Key1=value1&key2=value2
Some different notes about GET requests:
  • GET requests can be cached
  • GET requests continue to be in browser history
  • GET requests may be bookmarked
  • GET requests have to not be used when processing sensitive records
  • GET request has a duration limit
  • GET requests have to only be used to retrieve records

Simple url example: GET technique

The following is a easy URL that makes use of the GET approach to send parameters to the test.Cgi program:

/cgi-bin/check.cgi?call=Novice Tutorial &url=http://www.Welookups.Com

The following is the code for the take a look at.Cgi record:

Instance

#!/usr/bin/perl neighborhood ($buffer, @pairs, $pair, $name, $price, %FORM); # Read textual content statistics $ENV' REQUEST_METHOD' =~ tr/a-z/A-Z/; if ($ENV'REQUEST_METHOD' eq "GET") $buffer= $ENV'QUERY_STRING'; # study call/fee pair facts@pairs = cut up (/&/, $buffer ); foreach $pair (@pairs) ($name, $value) = split(/=/, $pair); $fee =~ tr/+/ /; $value =~ s/%( ..)/ %("C", Hex($1) )/eg; $FORM$name = $price; $name= $FORMname; $url= $FORMurl; print "Content-kind:text/htmlrnrn"; print "<html>"; print "<head>"; print '<meta charset="utf-8">'; print '<title> progaramming educational (welookups.Com)</identify>';print "</head>"; print "<body>"; print "<h2>$nameURL:$url</h2>"; print "</frame>"; print "</html>"; 1;

Simple form example: GET technique

The following is a shape that uses the GET approach to send two statistics to the server thru the HTML form. The submitted server script is also the take a look at.Cgi record. The check.Html code is as follows:

take a look at.Html file code

<!DOCTYPE html> <html> <head> <meta charset="utf-eight"> <name>progaramming Tutorial (welookups.Com)</title > </head> <frame> <shape movement="/cgi-bin/check.Cgi" method=" Get"> Site Name: <enter type="text" call="call"> <br /> Site URL: <input kind="text" call="url" /> <input kind="post" cost="Submit" /> </shape> </body> </html>

Transfer records the use of the POST method

Using the POST method to bypass statistics to the server is more comfortable and reliable. Some sensitive data consisting of consumer passwords want to apply POST to transfer information.

The following is also check.Cgi , which also can handle POST form records submitted by using the browser:

test.Cgi code

#!/usr/bin/perl neighborhood ($buffer, @pairs, $pair, $call, $price, %FORM); # Read text records $ENV' REQUEST_METHOD' =~ tr/a-z/A-Z/; if ($ENV'REQUEST_METHOD' eq "POST") read(STDIN, $buffer, $ENV'CONTENT_LENGTH'); else $buffer= $ENV'QUERY_STRING'; # study call/value pair facts @pairs = split (/&/, $buffer ); foreach $pair (@pairs) ($call, $value) = cut up(/=/, $pair); $fee =~ tr/+/ /; $price =~ s/%( ..)/ p.C.("C", Hex($1) )/eg; $FORM$name = $value; $call= $FORMname; $url= $FORMurl; print "Content-kind:text/htmlrnrn"; print "<html>"; print "<head>"; print '<meta charset="utf-8">'; print '<name> progaramming educational (welookups.Com)</name>';print "</head>"; print "<body>"; print "<h2>$nameURL:$url</h2>"; print "</body>"; print "</html>"; 1;

The following is a form that makes use of the GET method to ship two statistics to the server thru the HTML form. The submitted server script is also the take a look at.Cgi file. The take a look at.Html code is as follows:

test.Html Code

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <name>progaramming Tutorial (welookups.Com)</title > </head> <frame> <form movement="/cgi-bin/test.Cgi" approach=" Post"> Site Name: <input kind="text" name="name"> <br /> Site URL: <enter kind="textual content" call="url" /> <enter type="publish" cost="Submit" /> </shape> </body> </html>

passing checkbox facts through CGI software

checkbox is used to post one or more choice facts, the check.Html code is as follows:

take a look at.Html Code

<!DOCTYPE html> <html> <head> <meta charset="utf-eight"> <identify>progaramming Tutorial (welookups.Com)</name > </head> <frame> <form motion="/cgi-bin/test.Cgi" method=" POST" target="_blank"> <input type="checkbox" name="welookups.Com" fee="on" /> progaramming academic <enter type="checkbox" call="google" price="on" /> Google <enter type="submit" cost="Select Site" /> </shape> </body> </html>

The following is the code for the check.Cgi record:

test.Cgi code

#!/usr/bin/perl neighborhood ($buffer, @pairs, $pair, $name, $value, %FORM); # Read Information $ENV' REQUEST_METHOD' =~ tr/a-z/A-Z/; if ($ENV'REQUEST_METHOD' eq "POST") examine(STDIN, $buffer, $ENV'CONTENT_LENGTH'); else $buffer= $ENV'QUERY_STRING'; # examine call/value pair data @pairs = break up (/&/, $buffer ); foreach $pair (@pairs) ($name, $price) = cut up(/=/, $pair); $fee =~ tr/+/ /; $cost =~ s/%( ..)/ p.C.("C", Hex($1) )/eg; $FORM$name = $cost; if( $FORMwelookups.Com ) $welookups.Com_flag=" ON"; else $welookups.Com_flag=" OFF"; if( $FORMgoogle ) $google_flag=" ON"; else $google_flag=" OFF"; print "Content-kind:text/htmlrnrn"; print "<html>"; print "<head>"; print '<meta charset="utf-8">'; print '<title> progaramming educational (welookups.Com)</name>'; print "</head>"; print "<frame>"; print "<h2> progaramming tutorial selected country: $welookups.Com_flag</ H2>"; print "<h2> Google Select Status: $google_flag</h2> ;"; print "</body>"; print "</html>"; 1;





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.