?
Solved

cookies in a delphi cgi pgm

Posted on 2001-08-31
1
Medium Priority
?
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 7

Accepted Solution

by:
Motaz earned 150 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

Independent Software Vendors: 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

649 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