We help IT Professionals succeed at work.

Cookies in my App!

mirghani
mirghani asked
on
325 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.
Comment
Watch Question

Commented:
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.

Author

Commented:
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.

Author

Commented:
how we can store the username and password using Response.....,and it is coming as Request

Commented:
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

Commented:
The cookies will be stored on the client machine using the cookies in the response...

Commented:
Oh yeah,

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

Thats how

Commented:
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

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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.

Commented:
why did you accepted my comment, if it wasn't what you needed?

Author

Commented:
it was ok,but i asked for more details.
i think i was wrong u don't deseve that points ZIF:-(
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.