We help IT Professionals succeed at work.

cgi help with Delphi 6 Enterprise.

delpro
delpro asked
on
I want to create cgi programs with Delphi 6.
I want to know step for step how to do the folowing.
1. Read the url - if the user type www.mysite.com?123 i want to read 123
2. write 123 to a cookie that must expire in 3 days
3. Read the cookie.
4. Show the cookie on the website.

Then I need another program that read input varibels from a form (name and surname)
and to send it back to the user

Your name is %Name and surname is %surname

How does the login work?
If the user login and go to a webpage and then to another page how do i know that the user have log in when thay go to the second page?
Comment
Watch Question

Commented:
Hi delpro,
you will find all that in my EBook (Delphi programming guide), just install EBook and Delphi programming guide book then install it in the application, see Web development chapter.
www.geocities.com/motaz1

Motaz
Commented:
This is one topic of the chapter:

-----------------------

You can send a query or parameters to the CGI from your browser at Address bar such as:

//localhost/cgi/Para.exe?Test

In this case Test will be passed as a Query to Para.exe application. Another example is:

//localhost/cgi/Para.exe?Name=Motaz

In this case Name=Motaz will be passed.

Example:

- Start new CGI server application and add new item in at WebModule1 (Action).

- Click Save All button then name the WebModule as ParaF.pas, and the project as Para.dpr.

- At WebActionItem1 OnAction event write:

  Response.Content:=
  'Your query is: ' + Request.Query;

This will shows the query that sent from the browser. For example :

//localhost/cgi/Para.exe?Test

Will display:

  Your query is: Test

//localhost/cgi/Para.exe?Name=Motaz

Will display:

  Your query is: Name=Motaz

Reading Query property of Request object is suitable for short queries and single values, but if we want to pass many values we must use QueryFields property. Below example show you how to use QueryFields to get all passed variable names and values:

- At OnAction write:

var
  i: Integer;
  N: string;
  V: string;
begin
  (*** Get variables name ***)
  for i:= 0 to Request.QueryFields.Count - 1 do
    N:= N + Request.QueryFields.Names[i] + ', ';

  (*** Get variables value ***)
  with Request.QueryFields do
    for i:= 0 to Count - 1 do
      V:= V + Values[Names[i]] + ', ';

  Response.Content:= 'Variables name: <b>' +
    N + '</b><br>Variables value: <b>'
     + V + '</b>';


- Test this example with this parameters:

http://localhost/cgi/para.exe?Name=Motaz&Country=Sudan&Age=24

The result at your browser would be:

  Variables name: Name, Country, Age,
  Variables value: Motaz, Sudan, 24,

This example should work with any number of parameters.

If you are know what parameters name you will pass you can directly access it's value using the name such as:

var
  AName, Country: string;
  Age: Byte;
begin
  AName:= Request.Values['Name'];
  Country:= Request.Values['Country'];
  Age:= StrToInt(Request.Values['Age']);
  ....

Then you can add these fields to a database, or do any thing you want with it. The most important thing that this data now are captured by your server application.


Sending data from HTML page

In real Internet pages users should not call your CGI applications directly such as the method above, no one will write address like below:

http://localhost/cgi/para.exe?Name=Motaz&Country=Sudan&Age=24

If he did, he may miss some fields or write variable names wrongly. To avoid this problem you must design a normal HTML page using any HTML editors such as FrontPage Express and you can write some thing like this in front page:

<a href="../cgi-bin/FAQ.EXE?Q=1">What is the CGI</a>
<a href="../cgi-bin/FAQ.EXE?Q=2">How can I build server side applications</a>
<a href="../cgi-bin/FAQ.EXE?Q=3">How to set Paradox's master password at run time</a>

This code will appear in the browser as:

What is the CGI
How can I build server side applications
How to set Paradox's master password at run time

When the user clicks at the first link he/she will send Q=1 to FAQ.EXE and the CGI understands that the user want to read question number 1 so it can displays it.

Sending data using data fields controls

The previous methos used to send fixed data, which the designer of the page want to send it when the user clicks some links, but how can you let the user send it's own data?

The answer is very simple, only you can follow below steps:

1- Run HTML editor, for example FrontPage Express
2- Drop three Edit boxes, and a Submit button
3- A form will automatically be droped, right click this form then select Form Properties
4- Click Settings, on Action edit box write Para.exe
5- At Method combo box select Get and click Ok
6- Right click first Edit box then select Form Field Properties
7- At Name edit box write (Name)
8- Repeat step 6 and 7 for the rest of fields, name them (Age and Country)
9- Save this page at your CGI directory (in the same directory with Para.exe), name it Test.htm
10- Run the browser and write this address:

//localhost/cgi/Test.htm

Fill the fields then click submit or press enter, what did you say?

For example if you enter these data (Motaz, 24, Sudan) you will see at Address bar of the browser:

http://localhost/cgi/Para.exe?Name=Motaz&Age=24&Country=Sudan&B1=Submit

That mean when you use Get method all submitted fields will be displayed at Address bar, so that can you send a password field using this method?
Sure you cann't, and the alternative of Get method is Post method, but Post wouldn't work with this example (Para.dpr). The all things that you have to do is to replace QueryFields property of Request with ContentFields, and it should works with Post method, try it and see the difference. The result is that the fields will not be displayed any more at Address bar of the browser, so that it is more suitable to send sensitive data such as passwords. Moreover by using Post method you can send alot of data such as memos, but in Get method the amount of parameters are limited.

----------------------

Explore More ContentExplore courses, solutions, and other research materials related to this topic.