CGI Web Programming/zh CN

From Lazarus wiki
Jump to: navigation, 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.

这是CGI教程的小小开始,希望所有人做出贡献。

关于 CGI

CGI(通用通道界面) 并不复杂:

有两台计算机——客户端和服务器:

  • 服务器存储了要访问的文件(网页、图象和CGI)。
  • 客户端通常使用网络浏览器请求服务器上的文档。

只有你的服务器支持,你才能使用CGI。

After hosted, CGI can now be used normally as: http://www.yourserver.com[.xx]/yourlogin/cgi-bin/cginame.exe

Notes:

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

关于 FreePascal CGI 以及如何测试你的CGI

Free Pascal 的CGI程序是普通的可执行程序。

请注意,Free Pascal CGI不是解释执行的,而是编译执行的。

要测试你的CGI你必须在你的计算机上架设服务器,并且支持CGI.

我推荐一种优良的HTTP服务器 ——Apache 。 支持多种操作系统平台。 Another HTTP Server is Xitami that supports CGIs in perl CGI and 16bits (but Xitami is very archaic).

一个很简单的样例

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.