Solved

cookies in a delphi cgi pgm

Posted on 2001-08-31
1
646 Views
Last Modified: 2006-11-17
I need an example of how to write and read from a cookie using a Delphi stand-alone CGI.
0
Comment
Question by:fussell
1 Comment
 
LVL 7

Accepted Solution

by:
Motaz earned 50 total points
ID: 6445382
Setting cookies:

Cookies always must be set after logining. For example suppose that the user enter his name and password in a login form then he submit it to this CGI application:

var
  Login, Password: string;
begin

  // Read Login name and Password from Login form
  Login:=
    Request.ContentFields.Values['Login'];
  Password:=
    Request.ContentFields.Values['Password'];

  //....
  // After checking Login name and password in users Database
  // send Login name and Password to user's cookies

  Response.Cookies.Add;
  Response.Cookies.Items[0].Name:= 'Login';
  Response.Cookies.Items[0].Value:= Login;
  Response.Cookies.Items[0].Expires:= Now + 1;
 
  Response.Cookies.Add;
  Response.Cookies.Items[1].Name:= 'Password';
  Response.Cookies.Items[1].Value:= Password;
  Response.Cookies.Items[1].Expires:= Now + 1;

  Response.SendResponse;

This code of OnAction event will save current user Login and Password in his cookies to be used later in other requests.
Expires property set the expiration date of the cookie, for example if we set it to Now + 1 that means this cookie will not be sent with requests after a day since last setting for that cookie (Logining). Also you can set expiration date after an hour (Now + 1/24), see Date and time routines


Reading cookies:


Now after logining the user want to send requests such as asking a question or see his messages, etc...
The user will click in CGI link but this time he would not send the
Login name and Password again, because they are already stored in his cookies. The authentication of the CGI will be like the code below:

var
  Login, Password: string;
begin

  // Read cookies to check is the user already logged on

  Login:=
    Request.CookieFields.Values['Login'];
  Password:=
    Request.CookieFields.Values['Password'];

  //...
  // Check user login and password, if the fields are empty that
  // mean the user does not logged in. In this case you can
  // display login form:

  if Login = '' then
    Response.SendRedirect('Login.htm')
  else
  // Response to his request
  // ...


Notes:

For security purpose there are cookies for each CGI application, so that in your CGI application you cann't read other sites cookies which stored in your client computer, for example there are two different cookie storage place for below URLs:

http://www.yourserver.com/cgi-bin/users.exe
and
http://www.otherserver.com/cgi-bin/users.exe

And it seem that the URL is case sensitive so that below addresses are even has different cookies:

http://www.yourserver.com/cgi-bin/users.exe
and
http://www.YourServer.com/CGI-BIN/Users.exe

Anothe important thing is that you have to set and read cookies using the same CGI application.

Motaz
www.geocities.com/motaz1
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question