Difference between revisions of "CGI Web Programming"

From Lazarus wiki
Jump to navigationJump to search
Line 17: Line 17:
  
 
==About FreePascal CGI and how you can test your CGIs==
 
==About FreePascal CGI and how you can test your CGIs==
A CGI Free Pascal is a normal executable program, as any program this on your computer.
+
A CGI Free Pascal is a normal executable program, as any program in your computer.
Unlike the CGIs in Perl, Free Pascal CGI is not interpreted. It's a independent executable program.
+
Unlike the CGIs in Perl, Free Pascal CGI isn't interpreted. It's a independent executable program.
  
To test your CGI, you must have installed on your PC a '''HTTP Server''' with support for CGI.
+
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 the '''Apache''', available both for Windows and for Linux.
+
A good, and recommended, [http://en.wikipedia.org/wiki/HTTP_Server HTTP Server] is [http://en.wikipedia.org/wiki/Apache_HTTP_Server Apache], available both for Windows and Linux.
Another '''HTTP Server ''' is the '''Xitami''' that supports CGIs in perl CGI and 16bits (but '''Xitami''' is very archaic).
+
Another '''HTTP Server ''' is [http://en.wikipedia.org/wiki/Xitami Xitami] that supports CGIs in perl CGI and 16bits (but '''Xitami''' is very archaic).
  
 
==A very basic example==
 
==A very basic example==

Revision as of 02:03, 9 June 2008

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

About CGI

CGI (Common Gateway Interface) work simply:

We have two computers:

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

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

Notes:

  • The CGI file don't need have 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
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. 

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.