Solved

cookies in a delphi cgi pgm

Posted on 2001-08-31
1
640 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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
indy tidtcpclient threading issue 8 54
problem when i try to pack my dll file with upx 9 62
find a node in VST 2 46
LAN or WAN ? 11 58
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now