Jaymol
asked on
CGI and Cookies.
Hi.
Firstly, sorry about the low points but it's all I've got and I need this really.
I need to know how to write and read a cookie from within a CGI application.
Can anyone guide me please?
Thanks a lot,
John.
Firstly, sorry about the low points but it's all I've got and I need this really.
I need to know how to write and read a cookie from within a CGI application.
Can anyone guide me please?
Thanks a lot,
John.
ASKER
Thanks Geo, but I've been trying that (and similar stuff) all morning and haven't made a single cookie yet!
John.
John.
Jaymol, maybe you should go and ask the local bakery... ;-) (just joking)
Are you sure that no cookie is set? Did you enable cookies in thew browser? What happens if you do a request via good old telnet (assuming that the web server is running on your local machine; turn local echo to on if you want to see what you type):
telnet localhost 80
GET /yourdll.dll?login=Myself& passw=LetM eIn HTTP/1.0
(two returns!)
You should then get the reply from your DLL and see what is going wrong.
Are you sure that no cookie is set? Did you enable cookies in thew browser? What happens if you do a request via good old telnet (assuming that the web server is running on your local machine; turn local echo to on if you want to see what you type):
telnet localhost 80
GET /yourdll.dll?login=Myself&
(two returns!)
You should then get the reply from your DLL and see what is going wrong.
ASKER
It's actually a CGI application that I'm writing, and cookies are enabled as there are cookies for other sites.
Pulling my hair out about this!
John.
Pulling my hair out about this!
John.
Well,
There is one html page containing a request to your CGI (html link or form), where in response of that request (WebAction1) you must set the cookies and send a second html page in response. The second html page contains a request to your CGI again, and when processing that second request from the same client browser (WebAction2 for example) you must be able to read the cookies set in the WebAction1.
Regards, Geo
There is one html page containing a request to your CGI (html link or form), where in response of that request (WebAction1) you must set the cookies and send a second html page in response. The second html page contains a request to your CGI again, and when processing that second request from the same client browser (WebAction2 for example) you must be able to read the cookies set in the WebAction1.
Regards, Geo
ASKER
Okay, what I've done now is completely stripped the lot down. I've created a new cgi application and added this into the event of the one and only action (default action.)
With Response.Cookies.Add do begin
Name:='PageColour';
Value:='3';
Domain:='http://www.gadgetsntoys.com';
end;
Response.Content:='Cookie should be created now.';
I gave the action the path name of "test" and then uploaded the exe to my server.
Then, I ran the script with the following command...
http://www.gadgetsntoys.com/test.exe
It replied that the cookie had been created (just so I know it actually ran!), but there's no cookie.
HELP!!!!
John.
With Response.Cookies.Add do begin
Name:='PageColour';
Value:='3';
Domain:='http://www.gadgetsntoys.com';
end;
Response.Content:='Cookie should be created now.';
I gave the action the path name of "test" and then uploaded the exe to my server.
Then, I ran the script with the following command...
http://www.gadgetsntoys.com/test.exe
It replied that the cookie had been created (just so I know it actually ran!), but there's no cookie.
HELP!!!!
John.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If you look at the SetCookieField code, it's actually what I did. (SetCookieField is a function, coded within just like my example.)
John.
John.
procedure TWebResponse.SetCookieFiel d(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;
Actually, I think that the Path portion is essential (look at the Netscape docs), and the expiration should gbe in the future (like, Now+356 for one year's expiration time).
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
Domain := ADomain;
Path := APath;
Expires := AExpires;
Secure := ASecure;
end;
end;
Actually, I think that the Path portion is essential (look at the Netscape docs), and the expiration should gbe in the future (like, Now+356 for one year's expiration time).
ASKER
Okay, I had a look at the links and made a little exe.
Here's the two actions that were in it....
procedure TWebModule1.WebModule1WebA ctionItem1 Action(Sen der: 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;
procedure TWebModule1.WebModule1WebA ctionItem2 Action(Sen der: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
i: integer;
begin
Response.Content := '';
for i := 0 to (Request.CookieFields.Coun t - 1) do
begin
Response.Content := Response.Content + '' +
Request.CookieFields[i] + '';
end;
Response.Content := Response.Content + '';
end;
Basically, the first one creates the cookies, the second one reads them back.
There were no cookies created.
John.
Here's the two actions that were in it....
procedure TWebModule1.WebModule1WebA
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;
procedure TWebModule1.WebModule1WebA
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
i: integer;
begin
Response.Content := '';
for i := 0 to (Request.CookieFields.Coun
begin
Response.Content := Response.Content + '' +
Request.CookieFields[i] + '';
end;
Response.Content := Response.Content + '';
end;
Basically, the first one creates the cookies, the second one reads them back.
There were no cookies created.
John.
ASKER
From the netscape doc you gave me a link to...
"Expires is an optional attribute. If not specified, the cookie will expire when the user's session ends."
"If the path is not specified, it as assumed to be the same path as the document being described by the header which contains the cookie."
I understand cookies. I need to know how to create and read them from a Delphi CGI executable application.
"Expires is an optional attribute. If not specified, the cookie will expire when the user's session ends."
"If the path is not specified, it as assumed to be the same path as the document being described by the header which contains the cookie."
I understand cookies. I need to know how to create and read them from a Delphi CGI executable application.
Okay okay, don't get mad at me. I never use cookies but instead use a session ID I pass along with GET/POST requests. This allows to make server-side tracking without needing cookies, so I'm not an expert with cookies.
Have you done the test with TELNET? I tried your http://www.gadgetsntoys.com/test.exe url, but I get a 404...
Have you done the test with TELNET? I tried your http://www.gadgetsntoys.com/test.exe url, but I get a 404...
ASKER
?????
I'm not getting mad! And I deleted the test.exe
I'll put another one back in there, called test.exe so you can have a go at that. (It'll just be the code I supplied above.)
John.
I'm not getting mad! And I deleted the test.exe
I'll put another one back in there, called test.exe so you can have a go at that. (It'll just be the code I supplied above.)
John.
ASKER
I've put the test.exe in there. If you run it like this...
http://www.gadgetsntoys.com/cgi-bin/test.exe
...then it runs the default procedure that is the create cookie bit.
Thanks,
John.
http://www.gadgetsntoys.com/cgi-bin/test.exe
...then it runs the default procedure that is the create cookie bit.
Thanks,
John.
Ok, here's what is returned:
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Mon, 17 Sep 2001 14:55:51 GMT
Set-Cookie: PageColour=3
Content-Type: text/html
Content:
So I believe that the cookie is set.
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Mon, 17 Sep 2001 14:55:51 GMT
Set-Cookie: PageColour=3
Content-Type: text/html
Content:
So I believe that the cookie is set.
ASKER
But it's not!
John.
John.
Where and how are you checking this?
ASKER
Thanks for your help mate. Turns out that I was being a donkey and not checking for the cookies correctly. (I've been going mad over this all day, but it's been working all day!!!)
Thanks again,
John.
Thanks again,
John.
*lol* things like this happen...
Thanks for the points anyways...
Thanks for the points anyways...
ASKER
Thanks for the help. BTW, what d'you think of the site so far?
John.
John.
ASKER
How come this doesn't work then?
var
TmpS : String;
begin
TmpS:=Request.CookieFields .Values[Re quest.Quer yFields.Va lues['name ']];
Response.ContentType:='tex t/html';
Response.Content:='<b><u>C ookie Value</u></b><br><br>Name : '+Request.QueryFields.Valu es['name'] +'<br>Valu e : '+TmpS;
I'm running it to get back a cookie value I created earlier but it returns a server error instead.
John.
var
TmpS : String;
begin
TmpS:=Request.CookieFields
Response.ContentType:='tex
Response.Content:='<b><u>C
I'm running it to get back a cookie value I created earlier but it returns a server error instead.
John.
Here is my way of doing this:
Setting cookies :
{ login action }
procedure TWebModule1.WebModule1WebA
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
slCookies : TStringList;
login, passw : string;
begin
slCookies := TStringList.Create;
...
{ reading values from somewhere - from Request here }
login:=Request.ContentFiel
passw:=Request.ContentFiel
.....
{ adding cookies to string list }
slCookies.Add('login='+log
slCookies.Add('passw='+pas
{ set cookies in Response }
Response.SetCookieField(sl
.......
slCookies.Free;
end;
Reading cookies:
procedure TWebModule1.WebModule1WebA
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
login, passw : String;
begin
...
login := Request.CookieFields.Value
passw := Request.CookieFields.Value
...
end;
Regards, Geo