What Is CGI (Common Gateway Interface)? – POFTUT

What Is CGI (Common Gateway Interface)?

Web Servers are an important part of the internet and the web. Web servers used to serve web sites, web pages, APIs, documents, video images, etc. Web servers are generally designed to be dynamic to respond to user requests in a proper fashion. CGI (Common Gateway Interface) is a technology designed to provide dynamic programming and response capabilities to the web servers.

What Is CGI (Common Gateway Interface)?

CGI is the short form of the Common Gateway Interface. CGI provides the programmatic interface to the webserver where the webserver will consume the CGI scripts and applications in order to respond to the user request. It is called common because it provides a generic interface where it is used as a gateway to the different scripts and applications. CGI scripts generally produce web pages that contain HTML, JavaScript, CSS, Image, API, ASCII text, etc.

CGI is first created as a best practice in NCSA (National Center for Supercomputing Applications) in order to call command-line executables from the web. With wide usage and popularity, the standard RFC 3875 is created in November 1997.

CGI Scripts

CGI scripts mainly created using C programming language because of its standard uses C programming for definitions. getenv() function of the C library is used to get environment variables from the requests. CGI scripts are stored in /usr/local/apache/htdocs/cgi-bin path in general but the path can be changed according to will. CGI scripts can be used in different programming and scripting languages like Bash, Perl, Python, etc. but for easiness and practical development and change scripting languages Bash and Perl are preferred.

LEARN MORE  How To Specify Host, Port and Protocol For Tcpdump?

Example CGI Script

Below we can see an example CGI script which is developed in Perl scripting language.

  • #!/usr/bin/perl line is used to set the given file interpreter or programming language. This line will set the interpreter as Perl which is located at /usr/bin/perl.
  • =head1 DESCRIPTION is a comment which simply put a header to the documentation and below the lines are also comment
  • =cut is the end of the comment.
  • print "Content-Type: text/plain\n \n will output the first line of the HTTP response for the request. This line will output the content type as plain text.
  • for my $var ( sort keys %ENV) { block will iterate over the environment variables and put the value into $var and the $var will be printed in every step.

When we call this Perl CGI script we will get an output or HTTP response as HTML format like below.

Leave a Reply

Your email address will not be published. Required fields are marked *