Solved

Cookies in my App!

Posted on 2000-02-19
11
221 Views
Last Modified: 2010-04-04
my App Control users Discussion area after loging in and i want to use cookies in my App,as to store user Login and password once it is correct and never asks him to enter it again,and whenever i want to display his login on the page(e.g:this page which i'am writing the Ques on it i can find my name displayed at the top,i mean Experts Side),Pls if there is any benefit of using Cookies Rather Than This Specify it.
0
Comment
Question by:mirghani
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 10

Expert Comment

by:Jacco
ID: 2537938
You can use the Cookies property of the TWebResponse object like:

var
  Cookie: TCookie;

....

  Cookie := Response.Cookies.Add;
  Cookie.Name  := 'Username';
  Cookie.Value := 'MyName';
  Cookie := Response.Cookies.Add;
  Cookie.Name  := 'Password';
  Cookie.Value := 'xazuejfd';

  Response.Content := ...Content

Regards Jacco

P.S. You may want to set your cookie expiry because by default the cookie expires with the explorer session.
0
 

Author Comment

by:mirghani
ID: 2537952
hi jacco,
why don't u use Request.contentfields,or what it is purpose.
and that code seems to store the username and its password,but till when it is valid.
i want to get the answer as it can help me in getting the concept of cookies:-)
thanks.
0
 

Author Comment

by:mirghani
ID: 2537954
how we can store the username and password using Response.....,and it is coming as Request
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 10

Expert Comment

by:Jacco
ID: 2537964
check www.cookiescentral.com

for details on cookies.

The code i gave is for storing the cookie in the response.

You might consider the following:

procedure SomeActionHandler...
var
  bLogin: Boolean;
  bSendCookies: Boolean;
  sUsername, sPassword: string;
  Cookie: TCookie;
begin
  bLogin := False;
  bSendCookies := False;
  if (ContentFields.Values['UserName']<>'') and (ContentFields.Values['Password']<>'') then begin
    // user posted a login screen
    sUsername := ContentFields.Values['UserName'];
    sPassword := ContentFields.Values['Password'];
    // check user/name password
    bLogin := True; {or False}
    bSendCookies := bLogin;
  end;  
  if not bLogin and (CookieFields.Values['Username']<>'') and (CookieFields.Values['Password']<>'') then begin
    // user has a cookie
    // check user/name password
    bLogin := True; {or False}
  end;
end;
if bLogin then begin
  // send the login page
  Response.Content := ppLogin.Content;
end else begin
  // send the requested page
  Response.Content := ppAnyPageProducer.Content;
  if bSendCookies then begin
    // send the cookies if the user used login screen
    Cookie := Response.Cookies.Add;
    Cookie.Name := 'Username';
    Cookie.Value := sUserName;
    Cookie := Response.Cookies.Add;
    Cookie.Name := 'Password';
    Cookie.Value := sPassword;
  end;
end;

I have not experimented yet with the expiry...

Regards Jacco

P.S. I typed the code from the mind so it might contain some typos
0
 
LVL 10

Expert Comment

by:Jacco
ID: 2537996
The cookies will be stored on the client machine using the cookies in the response...
0
 
LVL 10

Expert Comment

by:Jacco
ID: 2537998
Oh yeah,

The cookies the client has for the URL it request are automatically send with the request.

Thats how
0
 
LVL 17

Expert Comment

by:geobul
ID: 2542209
Hi,

Here is my way:

1. Set cookies :

procedure TDBModule.DBModuleWebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  mCookie : TStringList;
  ....
begin
  mCookie := TStringList.Create;
  ...
  mCookie.Add('name='+sName);
  mCookie.Add('pass='+sPass);
  ...
  Response.SetCookieField(mCookie, Request.Host, Request.ScriptName, Request.Date, False);
  ...
  mCookie.Free;
end;

Look at SetCookieField method. Here you specify host, script, expiry date, security. Cookies are available for this host and script name only.

2. Read cookies:

....
sName := Request.CookieFields.Values['name'];
sPass  := Request.CookieFields.Values['pass'];
....

To read some cookies, they must be set before.

You can use cookies to store any information your app needs from the client. Once you have set a cookie, you can read it as many times as you wish. It is a permanent information but your app can replace the value with the new one.
For example it's a current page status information. It is stored under one name 'status' but receives new value on the next page. This way your app can determine from which state the request is comming.

Regards, Geo

0
 
LVL 8

Accepted Solution

by:
ZifNab earned 150 total points
ID: 2544188
here is the example code from Borland :

Working with cookies

 Technical Information Database

TI4523D.txt - Working with cookies

Category   :Internet/WEB
Platform   :All-32Bit
Product    :All32Bit,  

Description:
The purpose of this document is not to explain what a cookie is or what it is used for. Instead this document
assumes that you are familiar with the use of cookies and want to know how to work with them when
developing web server applications with Delphi 3.

The TWebRequest and TWebResponse objects provided in Delphi 3 both have properties which allow for
the easy use of cookies. TWebRequest has the Cookie and CookieFields properties to allow a web server
application to read the cookie header sent as part of a HTTP request. The TWebResponse object has the
Cookies property which allows a web server application to place a cookie on the client machine through the
cookie header of the HTTP response. This is usually set through the use of the SetCookieField method.

When a server responds to a HTTP request it sends a document with a header and a content section. Delphi
provides the ability to add a cookie header through the TWebResponse.Cookies property. This is best set
through the use of the SetCookieField method. The following TWebActionItem demonstrates the use of the
SetCookieField method to return a cookie to the requesting browser. This example uses localhost as the
domain. You would use this for testing and replace this string with your domain name. The third paramater is
a slash. This means that this cookie will be sent along with all requests from the browser while at this domain.
For further explainations of these paramaters see the aforementioned RFC 2109.

procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  MyCookies: TStringList;
begin
  MyCookies := TStringList.Create;
  with MyCookies do begin
    Add('Name=Frank Borland');
    Add('Address=100 Borland Way');
    Add('Product=Delphi');
  end;
  with Response do begin
    SetCookieField(MyCookies, 'localhost', '/', (Now + 1), False);
    Content := 'Cookie planted';
  end;
end;

When a request is made of the HTTP server the client browser issuing the request will package up all of the
aplicable cookie name value pairs and include them in the HTTP requests cookie header. Delphi 3 will make
these available to a web server application in two ways. The first way is as a string through the Cookie
property of the TWebRequest paramater of a TWebActionItem. It is also available as a TStrings property
called CookieFields. CookieFields is the parsed contents of the Cookie header of an HTTP request message.

The following are TWebActionItems which extract the name value pairs and return them to the client in the
form of a bare bones HTML page. The first example returns the cookie as a single string, while the second
example returns each name value pair on a seperate line.

procedure TWebModule1.WebModule1WebActionItem2Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  Response.Content := '' + Request.Cookie +
    '';
end;

procedure TWebModule1.WebModule1WebActionItem3Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  i: integer;
begin
  Response.Content := '';
  for i := 0 to (Request.CookieFields.Count - 1) do
  begin
    Response.Content := Response.Content + '' +
    Request.CookieFields[i] + '';
  end;
  Response.Content := Response.Content + '';
end;

//end TI

Regards, Zif.
0
 

Author Comment

by:mirghani
ID: 2545065
thanks Zifnab,
but i want to know how can i control users from my App using login+pssword for the rest of pages once he login,i mean that my App can check automatically if the username and password is valid,then i can make use of cookies,i hope u got it.
thanks.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2545360
why did you accepted my comment, if it wasn't what you needed?
0
 

Author Comment

by:mirghani
ID: 2545737
it was ok,but i asked for more details.
i think i was wrong u don't deseve that points ZIF:-(
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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