in reply to Re: Cookies write to screen, not to cookie file
in thread Cookies write to screen, not to cookie file

I've tried to use the modules, without success. My own ignorance is undoubtedly responsible. I'm still trying to learn Perl, after several years of writing scripts, and I haven't studied the modules.

I tried to limit the amount of information that I included in my original problem declaration because I know that much of it would be superfluous, but here's a little background.

The application is a shopping cart system. The main script, named kart.cgi, does contain a  content command line. Most of the output takes place in another script, called config.cgi. The latest script is called register.cgi, and contains no  print statements. All information is passed to config for output.

All of the subroutines for registration are in the register.cgi script, but control remains with kart.cgi. I thought that by using the  require command, the  content would be negated when the subroutines were accessed, but apparently not. :-)

Until the visitor inputs the information that I am trying to output with the cookie, how can I write it to his or her cookie file? Ergo, I need to collect the data before I can write it out. If I was simply creating a visit count, or something like that, I could write the cookie as soon as the kart.cgi is opened.

To the best of my knowledge, the \r\n at the end of a print statement is only important as a delimiter when viewing source code. Is there another reason why it should be in the  Set-cookie: command line?

Replies are listed 'Best First'.
Re^3: Cookies write to screen, not to cookie file
by chromatic (Archbishop) on Jul 01, 2004 at 23:42 UTC
    I need to collect the data before I can write it out.

    Yes, that is true. However, this means that the user will have to click a link that sends the information or submit a form that sends the information before you can set a cookie.

    You need some logic in your script that can detect whether the user already has a cookie, has just submitted data that you can use to set a cookie, or has no cookie or cookie-making data. Only then can you solve this problem.

    Also keep in mind that you cannot set a cookie and read its values in the same invocation. I think this is what's confusing you most right here. CGI programming is rather like a conversation between two very polite people who never interrupt and wait for a complete response before answering back.

Re^3: Cookies write to screen, not to cookie file
by sgifford (Prior) on Jul 02, 2004 at 15:20 UTC
    As chromatic says, you need to display the form as one Web page, then submit it to a script that will output the cookie headers. That script will need to parse the information the user entered, then output the headers (including the cookie headers), then output the body of the Web page.

    As for line endings in the headers, they are important, though whether you should use "\n" or "\r\n" may be Web server dependent (I usually use "\n", and assume my Web server will change it to a protocol-appropriate line ending). Line endings are how the Web server and browser know that one header has ended and the next has begun.