[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

cookie problem?

Posted on 1999-12-26
6
Medium Priority
?
228 Views
Last Modified: 2010-04-04
procedure TWebModuleWord.WebModuleItemLoginAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  UserName,UserPwd:string;
  Tempstr:tstrings;
begin
  if request.methodtype=mtpost then
  begin
    UserName:='';UserPwd:='';
    TempStr:=TStringList.Create;
    request.ExtractContentFields(TempStr);
    UserName:=GetValueOfPost(TempStr.Strings[0]);
    UserPwd:=GetValueOfPost(TempStr.Strings[1]);
    TempStr.Free;
    //Ìí¼Ócookie
    with Response.Cookies.Add do
    begin
      Name:='UserName';
      Value:=UserName;
    end;
    with Response.Cookies.Add do
    begin
      Name:='UserPwd';
      Value:=UserPwd;
    end;
    Response.SendRedirect('http://hubdog/hubdog.exe/hubdog');
    end;
  end;
end;

procedure TWebModuleWord.WebModuleWordItemMainAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  UserName:string;
begin
  if request.CookieFields.Values['UserName']<> '' then
  begin
    PageDynamic.HTMLFile:=ExtractFilePath(Paramstr(0))+'Test.htm';
    Response.content:=PageDynamic.Content;
  end
  else
    Response.SendRedirect('http://hubdog/happyword/happyword.exe/Login');
end;

After having added two cookies and sendredirect to MainActionItem,
the MainActionItem can not fetch the cookies added before and return to loginitem.
why and how to solve the problem?

thank you very much

hubdog
0
Comment
Question by:hubdog
  • 3
  • 3
6 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 2306514
Response.Cookies.Add does not work.
Store the cookies in a StringList (e.g. MyCookies):

Response.SetCookieField(MyCookies, '', '/', Now + 1, false);


Eps.
0
 
LVL 2

Author Comment

by:hubdog
ID: 2306554
hi Epsylon
procedure TWebResponse.SetCookieField(Values: TStrings; const ADomain,
  APath: string; AExpires: TDateTime; ASecure: Boolean);
var
  I: Integer;
begin
  for I := 0 to Values.Count - 1 do
    with Cookies.Add do
    begin
      Name := Values.Names[I];
      Value := Values.Values[Values.Names[I]];
      Domain := ADomain;
      Path := APath;
      Expires := AExpires;
      Secure := ASecure;
    end;
end;
  I look the source of setcookiefield and didn't found any important difference from my code.
  And I follow your suggestion like this
    Cookies:=TStringList.Create;
    Cookies.Add('UserName=hubdog');
    Cookies.Add('UserPwd=Sysoft');
    Response.SetCookieField(Cookies, '', '/', Now+1000, false);
and not effect.

Could you explain it more clearly

thanks in advance

hubdog

0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2306577
It should work. Doesn't it write a cookie to your windows\cookies directory?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:hubdog
ID: 2306917
hi Epsylon
  I think the main problem is if I use response.content:='login correctly' then no problem.And if I use response.sendredirect('http://xxx') after having added cookie,then I can't add cookie successfully. Could you test it and tell me why?

thanks in advance

hubdog
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2306968
Yep, it won't work with SendRedirect. Here's something about it that I found a while ago:

-----------------------------------------------------

Here is my solution for checking the cookies.
In the code I wrote
---
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var cookielist : TstringList;
begin
  cookielist := TStringList.Create;
  try
    cookielist.Add('Name1=alice');
    Response.SetCookieField(cookielist,'','',-1, false);
  finally
    cookielist.free;
  end;
  Response.Content := '<html><head><meta http-eqiv="Refresh"
Content="0;URL=http://mywebsite/mydll.dll/checkcookie"></head></html>';
  Handled := true;
end;

procedure TWebModule1.WebModule1WebActionItem3Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var s : string;
begin
  s := '';
  try
    s := Request.CookieFields.Values['Name1'];
  except
    {silent}
  end;
  if Length(s) <> 0 then
    Response.SendRedirect('http://mywebsite/myhtml.htm')
  else
    Response.Content := 'Sorry, You do not accept the cookies.';
end;
---------
Since the SetCookieFields doesn't work with SendRedirect, I display the
temperary pape. Then the temperary page will refresh in 0 second and run the
mydll.dll/checkcookies,redirect the page.
It works for IE4,5 and NS4 from PWS.  It seems ASP doing the same way.
*************************
Still question:
from delphi4 help file, it says

procedure SendRedirect(const URI: string); virtual; abstract;
Description
Call SendRedirect to redirect the web client request to another URI that is the
proper target of the request. SendRedirect does the same thing as setting the
StatusCode to 301, the Location to URI, and calling SendResponse.

But I wrote the code as
  Response.StatusCode:=301;
  Response.Location := 'http://mywebsite/myhtml.htm';
  Response.SendResponse;
---
It doens't work for me at all. I guest the help file is wrong.

------------------------------------

"Hadi Hariri (Team Winshoes)" wrote:

> Well, you already sent me an e-mail, so I'll be brief:
>
> You have to use SendRedirect to redirect. Problem: Not compatible with
> SetCookieField. I have been trying to do a workaround using SetCustomHeader
> and Location and StatusCode props of response. More news I'll let you know.
>
> Alice Lius escribió en mensaje <379C9DC7.CED9F387@yahoo.com>...
> >I have to use SendResponse after SetCookieFields.   So how I redirect the
> >repoonse to another entry point in my dll.  Please help.
> >
> >Hadi Hariri wrote:
> >
> >> Well, one way I can think of is that once you set the cookie, redirect
> the
> >> response to another entry point in your dll to read the cookies. If the
> >> cookies are present then redirect to the original page you were intending
> to
> >> redirect after placing cookies. If not then display an error.
> >>
> >> Alice Liu escribió en mensaje <3798D6F5.7EEECDCA@yahoo.com>...
> >> >Somebody can help this.
> >> >If I create cookies from the login screen, how the .dll know if the user
> >> >accept this cookies or not.
> >> >---
> >> >alice
> >> >
0
 
LVL 2

Author Comment

by:hubdog
ID: 2307067
thank you very much!!!

hubdog
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

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

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…
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 video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses
Course of the Month7 days, 15 hours left to enroll

607 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