CGI Web Programming

From Lazarus wiki
Jump to navigationJump to search

English (en) español (es) français (fr) Bahasa Indonesia (id) русский (ru) 中文(中国大陆)‎ (zh_CN)

This is the start of a tutorial about cgi web programming. Everyone is invited to contribute.

About CGI

CGI (Common Gateway Interface) works simply as follows:

We have two computers:

  • Server Computer - This computer will store the hosted files (web pages, images and CGIs);
  • Client Computer - Generally, this computer has a browser to request the files hosted in server computer.

You can use CGIs only if your hosting server has CGI support. After hosting your files, CGI can now be used normally as: http://www.yourserver.com[.xx]/yourlogin/cgi-bin/cginame.exe

Notes:

  • The CGI file doesn't need the .exe extension.
  • The CGI should be (obviously) compiled for the platform of the computer server (Linux, Windows...)

About FreePascal CGI and how you can test your CGIs

A CGI Free Pascal is a normal executable program, as any program in your computer. Unlike the CGIs in Perl, Free Pascal CGI isn't interpreted. It's a independent executable program.

To test a CGI, you must have installed on your PC a HTTP Server with support for CGI. A good, and recommended, HTTP Server is Apache, available both for Windows and Linux. Another HTTP Server is Xitami that supports CGIs in perl CGI and 16bits (but Xitami is very archaic).

A very basic example

Here is an example of a minimal CGI program to demonstrate...

  1. Setting a cookie
  2. Outputting the content-type (ie make it put out legal text for HTTP)
  3. Reading Cookies
  4. Reading form data via GET
  5. Reading form data via POST

<delphi> program mini;

uses dos;

var

 a:string;
 c:char;

begin

 //set a cookie (must come before content-type line below)
 //don't forget to change the expires date

writeln('Set-cookie:widget=value; path=/; expires= Mon, 21-Mar-2005 18:37:00 GMT');

 //output legal http page
 writeln('Content-Type:text/html',#10#13);
 //demonstrate get cookies
 a:= GetEnv('HTTP_COOKIE');
 writeln('cookies:',a);
 
 //demonstrate GET result
 a:=;
 a:= GetEnv('QUERY_STRING');
 writeln('GET: ',a);
 //demonstrate POST result 
 a:=;
 while not eof(input) do
 begin
    read(c);
    a:= a+c;
 end;	 
 writeln('POST: ',a);

end. </delphi>

A simple hitcounter

The application hitcounter presents a simple hitcounter for your website. It is possible to enter blocklists in a configfile so robots and perhaps your own computer are excluded from the statistics. There are still a lot of improvements possible ofcourse like setting a cookie so you get only one hit per session, but it gives a good introduction into CGI programming. If you use the code and modify it, please send me a patch so I can update it.

You can find the code here: [1]


Existing CGI Frameworks

There are existing CGI units and frameworks that make working with CGI trivial (and allow one to set cookies, sessions, retrieve POST and GET). See Powtils

There is also a CGIModule for lazarus, and a few CGI and HTML units in the freepascal FCL.

Another CGI and FastCGI framework for FreePascal/Delphi is ExtPascal an Ext JS wrapper.