Page 1 of 1

Can't get cookies working

Posted: Fri Oct 05, 2007 2:22 pm
by cormullion
I can't get cookies working. The code I have is this:

Code: Select all

(load "...cgi.lsp")
(CGI:set-cookie "password" "secret" "asite.com" "/somedir")

(print "Content-type: text/html\n\r\n\r")
If I swap these two lines round, I can see the password/secret/asite stuff at the top of the page, but when they're the right way round no cookies are sent.

Any ideas for getting it working?

Posted: Fri Oct 05, 2007 4:11 pm
by rickyboy
What about ending the line with \r\n\r\n instead of \n\r\n\r? Does that make a difference? (I'm not trying to be cute, I just am not in a position to test that.)

Posted: Fri Oct 05, 2007 4:14 pm
by rickyboy
Also, is it possible to load the file ...cgi.lsp? I mean three dots, then cgi.lsp? While it is possible to name a file this way, it might not be what you meant.

Posted: Fri Oct 05, 2007 4:49 pm
by cormullion
Yes, that was just shorthand - I didn't put the full path of cgi.lsp in, since I've got it stored somewhere else. But I think it's working OK - the rest of the CGI stuff is working fine... Just can't get any cookies to appear.

Posted: Fri Oct 05, 2007 4:54 pm
by rickyboy
cormullion wrote:Yes, that was just shorthand - I didn't put the full path of cgi.lsp in, since I've got it stored somewhere else.
Oh, sorry about that. Well, I guess that leaves me tapped. :-(

Posted: Sat Oct 06, 2007 8:02 am
by Lutz
Here is a small working cookie example:

http://newlisp.org/code/cookie.cgi

The first time you go to the page it will say "No cookie found", the second time it will show the cookie.

here the source:

Code: Select all

#!/usr/bin/newlisp

(load "cgi.lsp")

(CGI:set-cookie "key" (uuid) "newlisp.org" "/code")

(print "Content-type: text/html\r\n\r\n")

(if (CGI:get-cookie "key")
	(println "<h2>Cookie found:" (CGI:get-cookie "key") "<h2>")
	(println "<h2>No cookie found</h2>"))

(exit)
Lutz

ps: for an example using cookie expiration time see the calendar application on the http://newlisp.org/index.cgi?Code_Contributions page

Posted: Sat Oct 06, 2007 12:20 pm
by cormullion
As I feared, it's not the cookie code, but something else. Your example works fine, but when copied to another place produces the error:

Code: Select all

list expected in function lookup : cookies called from user defined function CGI:get-cookie

Posted: Sat Oct 06, 2007 12:40 pm
by Lutz
There was a problem in cgi.lsp v.2.1 and earlier when '=' signs where present in the cookie key or value string. This has been fixed in cgi.lsp version 2.2 distributed since 9.2.1.

What version of cgi.lsp are you using?

Lutz

ps: 'cookies' is a variable local to the CGI name space as CGI:cookies and set to '() if no cookies are present.

Posted: Sat Oct 06, 2007 8:35 pm
by cormullion
Ah - your file doesn't work when copied to the server - says 'no cookie found' each time. So presumably there's something on the server that's preventing it working...

Edit: i now suspect that the expiry date is compulsory. Will investigate.

Edit: I think its working. Domain name had two dots perhaps.