Difference between revisions of "CGI Web Programming"

From Lazarus wiki
Jump to navigationJump to search
(Thanks to Mike Price, taken from http://community.freepascal.org:8080/bboards/message?message_id=149360&forum_id=24083)
 
m
Line 21: Line 21:
 
begin
 
begin
 
   //set a cookie (must come before content-type line below)
 
   //set a cookie (must come before content-type line below)
  writeln('Set-cookie:widget=value; path=/; expires= Mon 21-Mar-
+
writeln('Set-cookie:widget=value; path=/; expires= Mon, 21-Mar-2005 18:37:00 GMT');
2005');
 
  
 
   //output legal http page
 
   //output legal http page
 
   writeln('Content-Type:text/html',MESSAGE KEY MISSING: '10'13);
 
   writeln('Content-Type:text/html',MESSAGE KEY MISSING: '10'13);
+
 
 
   //demonstrate get cookies
 
   //demonstrate get cookies
 
   a:= GetEnv('HTTP_COOKIE');
 
   a:= GetEnv('HTTP_COOKIE');

Revision as of 18:45, 14 January 2006

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

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)
writeln('Set-cookie:widget=value; path=/; expires= Mon, 21-Mar-2005  18:37:00 GMT');

  //output legal http page
  writeln('Content-Type:text/html',MESSAGE KEY MISSING: '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.