Solved

Cookies in my App!

Posted on 2000-02-19
11
216 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
Comment Utility
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
Comment Utility
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
Comment Utility
how we can store the username and password using Response.....,and it is coming as Request
0
 
LVL 10

Expert Comment

by:Jacco
Comment Utility
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
Comment Utility
The cookies will be stored on the client machine using the cookies in the response...
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Expert Comment

by:Jacco
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
why did you accepted my comment, if it wasn't what you needed?
0
 

Author Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

15 Experts available now in Live!

Get 1:1 Help Now