Difference between revisions of "CGI Web Programming"
From Free Pascal wiki
Jump to navigationJump to searchLine 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) | ||
+ | //don't forget to change the expires date | ||
writeln('Set-cookie:widget=value; path=/; expires= Mon, 21-Mar-2005 18:37:00 GMT'); | writeln('Set-cookie:widget=value; path=/; expires= Mon, 21-Mar-2005 18:37:00 GMT'); | ||
Line 28: | Line 29: | ||
//demonstrate get cookies | //demonstrate get cookies | ||
a:= GetEnv('HTTP_COOKIE'); | a:= GetEnv('HTTP_COOKIE'); | ||
− | writeln(' | + | writeln('cookies:',a); |
− | cookies:',a); | ||
//demonstrate GET result | //demonstrate GET result | ||
a:=''; | a:=''; | ||
a:= GetEnv('QUERY_STRING'); | a:= GetEnv('QUERY_STRING'); | ||
− | writeln(' | + | writeln('GET: ',a); |
− | GET: ',a); | ||
//demonstrate POST result | //demonstrate POST result | ||
Line 44: | Line 43: | ||
a:= a+c; | a:= a+c; | ||
end; | end; | ||
− | writeln(' | + | writeln('POST: ',a); |
− | POST: ',a); | ||
end. | end. | ||
</pre> | </pre> | ||
− | |||
==A simple hitcounter== | ==A simple hitcounter== |
Revision as of 15:42, 12 May 2007
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...
- Setting a cookie
- Outputting the content-type (ie make it put out legal text for HTTP)
- Reading Cookies
- Reading form data via GET
- 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]