PERL Cookbook for CGI
!!! Warning !!!
Most of the providers don't allow CGI/Perl scripts
on their customers website to prevent some security failure.

Introduction
CGI n'est pas un langage de programmation, mais une interface entre votre navigateur et le serveur web sur lequel est exécuté un programme CGI, appelé également script CGI. Voici son fonctionnement :

  • Le visiteur remplit un formulaire HTML de manière habituelle, pour définir sa requête.
  • Dès que le visiteur clique sur le bouton Rechercher, les données du formulaire sont transmises au serveur web.
  • Le serveur est averti qu'il doit exécuter un programme particulier, le script CGI. Les données du formulaire sont alors transmises à ce programme.
  • Le programme CGI exploite les données du formulaire et recherche par exemple les informations adéquates dans une base de données.
  • Le programme génère alors un nouveau fichier .html temporaire à partir des résultats de la base de données, pour pouvoir transmettre le résultat de la requête de l'utilisateur.
  • Le fichier n'est pas enregistré, mais transmis directement, ligne à ligne, au navigateur qui l'affiche comme une page web normale. Le navigateur ne peut en principe pas différencier un fichier .html normal du résultat d'une requête CGI.
Le langage de programmation utilisé pour écrire le programme CGI est totalement indifférent. Le langage interprété PERL s'est cependant imposé comme standard pour CGI.
CGI is not a programming language, but an interface between your browser and the web server on which is executed a CGI program, also called CGI script. Here is how it works :

  • The visitor fills an HTML formulary as usual, to define his request.
  • As soon as the visitor clicks on the Search button, the formulary data are transmitted to the web server.
  • The server is warned that it has to execute a specific program, the CGI script. Then the formulary data are transmitted to this program.
  • The CGI program uses these formulary data to search, for example, usefull informations in a database.
  • Then the program generates a new .html temporary file from the results of the database to be able to transmit the result of the user request.
  • This file is not saved but directly transmitted, line to line, to the browser which displays it as a normal web page. The browser usually can not make any difference between a normal .html file and a CGI request result.
The programming language used to write the CGI program is completely meaningless.However, the interpreted language PERL is now a standard for CGI.
You can download PERL for Windows (Win32) at : http://www.activestate.com/ (8.8 Mo)
You can download a WebServer for Windows at : http://www.xitami.com/ (706 Ko)

Your first program in PERL
Type the following lines :
#!/usr/bin/perl
print "Bienvenue sous Perl !\n"
and then execute it by the command : perl test.pl (after saving it).

Identify the user thanks to a PERL script
Type the following lines :
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print <<html;
<HTML>
<HEAD>
  <TITLE> Variables d'environnement CGI </TITLE>
</HEAD>
<BODY>
Ce que je sais sur vous : <P>
<TABLE border=1>
html
foreach $key(sort keys %ENV)
{
  print "<TR> <TD> <B> $key </B>  </TD>\n";
  print " <TD> $ENV{$key}  </TD> </TR>\n";
}
print <<html;
</TABLE>
</BODY>
</HTML>
html
exit;

and then execute it by the command : perl mytest.pl > mypage.html (after saving it).
This program shows out the caracteristic of the user's computer. For example, in my case :

BLASTER A220 I5 D1 H5 P330 T6 E620
CGI_ROOT C:/Mes Documents/MonSiteConstr/cgi-bin
CGI_STDERR cgierr.log
CGI_STDIN C:\WINDOWS\TEMP\pipe0002.cgi
CGI_STDOUT C:\WINDOWS\TEMP\pipe0002.cgo
CGI_URL /cgi-bin
CMDLINE WIN
COMSPEC C:\WINDOWS\COMMAND.COM
CONTENT_LENGTH 0
CONTENT_TYPE
DOCUMENT_ROOT C:\Mes Documents\MonSiteConstr
GATEWAY_INTERFACE CGI/1.1
HTTP_ACCEPT application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE fr
HTTP_AUTHORIZATION Basic c3lsdm1hcmw6YmFiN2dpbmE=
HTTP_CONNECTION Keep-Alive
HTTP_CONTENT_LENGTH 0
HTTP_HOST 127.0.0.1
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
MIDI SYNTH:1 MAP:E MODE:0
PATH C:\PROGRAM FILES\PERL\BIN\;C:\PROGRA~1\ULTRAE~1;C:\WINDOWS;C:\WINDOWS\COMMAND
PATH_INFO
PATH_TRANSLATED C:\Mes Documents\MonSiteConstr
PROMPT $p$g
REMOTE_ADDR 127.0.0.1
REMOTE_HOST 127.0.0.1
REMOTE_USER sylvmarl
REQUEST_METHOD GET
SCRIPT_NAME /cgi-bin/quiestu.pl
SCRIPT_PATH C:/Mes Documents/MonSiteConstr/cgi-bin
SERVER_NAME 127.0.0.1
SERVER_PORT 80
SERVER_PROTOCOL HTTP/1.0
SERVER_SOFTWARE Xitami
SERVER_URL http://127.0.0.1/
SERVER_VERSION 2.4d9
SOUND C:\PROGRA~1\CREATIVE\CTSND
TEMP C:\WINDOWS\TEMP
TMP C:\WINDOWS\TEMP
WINBOOTDIR C:\WINDOWS
WINDIR C:\WINDOWS
 [Menu]
Last Update
02/10/2023
46206 visitors
850589 robots
since 01/01/2003
Page generated
in 1.24 seconds
💗 2003-2024 by S. MARLIERE. Copying is an act of love. Love is not subject to law. Please copy and share.