Cgi tutorial

<< Prev
Next >>

Introduction

CGI (Common Gateway Interface) is a protocol for web servers through which they can run programs (scripts) and can generate dynamic web pages.

Whether the web server (eg Apache) runs on Linux or Windows, these scripts will be executed if they are in the cgi-bin directory.
The language used to write these scripts is Perl and they are executed with the Perl interpreter (in Windows: perl.exe). A Perl script has the .pl (or .cgi) extension, and the first line of code will point the path to the interpreter.
- For Linux:


#!/usr/bin/perl


- For Windows:


#!"C:\xampp\perl\bin\perl.exe"


Perl (Practical Extraction and Reporting Language) is a programming language created in 1987, especially used for word processing, but it is now used in web applications and can interact with other programming languages.
Perl is a high-level language, meaning easy to understand by users, but hard to understand PCs. Using the interpreter, which is a program that converts high-level language, into a low-level language that the PC recognizes and can execute its instructions.
The major advantage is that Perl is extremely portable. It can run on any operating system that has the Perl interpreter installed.


Installing Perl in Linux

If the Linux distribution does not have Perl preinstalled, open the Terminal and type:


sudo apt-get install perl


and the path to the interpreter, which will be the first instruction in any perl program, will be:
#!usr/bin/perl


Installing Perl in Windows

Perl can also be downloaded and installed in Windows:


Perl

But the easiest way to write and run Perl scripts is to use Xampp, which has the Apache (and other servers) web server installed and has already set up the Perl interpreter.
Xampp has a control panel from where to manage servers.


Perl

All we have to do is write a perl script, put it in the directory: cgi-bin and start the Apache server. Then we access it with the browser, the script is executed and the result will be displayed in the browser.


Perl

Perl scripts

Let's test the simplest hello script. We write this script in a text editor, save it as: hello.pl and put it in the directory: cgi-bin.


Perl

To run it, we open the web browser and type:


localhost/cgi-bin/hello.pl


The result in the browser will be:


Perl
<< Prev
Next >>

Visit my websites:

https://www.jwebsaints.com
https://www.jwebplants.com