Perl CGI Programming
H2>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.
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- Span>script .cgi . pl .py
First CGI application
Here we make an investigate at.Cgi archive with the accompanying code:
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
The following table describes the information commonly used by HTTP headers in CGI programs:
|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: Date||Reserved resource last modified time|
|Content-length: N||Requested 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:
|CONTENT_TYPE||The 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_LENGTH||If 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_COOKIE||COOKIE content inside the client.|
|HTTP_USER_AGENT||Provides purchaser program records that comprises of the amount of varieties or other exclusive data.|
|PATH_INFO||The 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_STRING||If 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_ADDR||The 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_FILENAME||Full way to CGI script|
|SCRIPT_NAME||The call of the CGI script|
|SERVER_NAME||This is the host name, assumed name or IP manage of your web server.|
|SERVER_SOFTWARE||The 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:
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:
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=value2Some 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:
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
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:
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:
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
The following is the code for the check.Cgi record: