• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

About WebServer Application

I want to make web server application, but how could i make a cookie and read it?
Is there a forum example? pls mail to:
cheng-jian@163.net
0
chengjian
Asked:
chengjian
  • 5
  • 4
1 Solution
 
EpsylonCommented:
0
 
chengjianAuthor Commented:
I have seen that
0
 
EpsylonCommented:
So what's the problem then?
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
chengjianAuthor Commented:
for my poor englisn, not finish test yet, sorry:(
0
 
chengjianAuthor Commented:
i finished .
but i still not clear about those cookies related function and procedure, could discribe them for me?
and is there a forum example?
0
 
EpsylonCommented:
Here is what I did a few weeks ago:

// This method checks if a cookie has been received and takes the
// appropriate action.
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  if Request.CookieFields.Count = 0 then
    // redirect
    Response.SendRedirect('/login.htm')
  else begin
    // display the cookie contents
    Response.Content := Request.CookieFields.Text;
  end;
end;

// This method tries to plant the received ContentField values into a cookie
procedure TWebModule1.WebModule1WebActionItem2Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  MyCookies: TStringList;
  i: integer;
begin
  MyCookies := TStringList.Create;
  // Filter the name and passwork values out of the ContentFields
  with MyCookies do begin
    i := Request.ContentFields.IndexOfName('name');
    if i <> -1 then
      Add(Request.ContentFields.Strings[i]);
    i := Request.ContentFields.IndexOfName('password');
    if i <> -1 then
      Add(Request.ContentFields.Strings[i]);
  end;
  if MyCookies.Count >= 2 then begin
    // Plant the cookie
    with Response do begin
      SetCookieField(MyCookies, '', '/', Now + 30, false);
      Content := 'Welcome ' + MyCookies.Values['name'] + ', your cookie has been planted';
    end;
  end;
  MyCookies.Free;
end;

// This method deletes a cookie
procedure TWebModule1.WebModule1WebActionItem3Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  with Response do begin
    SetCookieField(Request.CookieFields, '', '/', -1, false);
    Content := Request.CookieFields.Values['name'] + ', your cookie has been deleted';
  end;
end;


Regards,

Epsylon.
0
 
chengjianAuthor Commented:
wait one day pls:-)
0
 
chengjianAuthor Commented:
hi, a day is not needed:)
discribe these pls:
SetCookieField(Request.CookieFields, '', '/', -1, false);
1.What's all these params means?
2.Response.Content := Request.CookieFields.Text; //what would be return in this example?
Could u explain all replated object/mathod/property for me? just a short discription, thanx

0
 
EpsylonCommented:
SetCookieField(Request.CookieFields, '', '/', -1, false);

parameters:
1) the cookie list
2) the domain, '' (empty string) means local host
3) the path within the domain the cookie is set for, '/' means the entire domain
4) the expiration date on which the cookie expires, -1 deletes the cookie right away
5) whether or not to use a secure connection to send the cookie to the server



Response.Content := Request.CookieFields.Text;

In this line the webserver app returns the received cookie to the client as text. It's just to see what happens when the the webserver app receives a cookie.

Eps.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now