in reply to Re^3: Question regarding CGI and cookies
in thread Question regarding CGI and cookies

Don't think in terms of "the cookie" - that obscures what is really going on. Think in terms of a) a cookie FILE - a file on a user's disk, b) a cookie VARIABLE - a temporary value in your script, c) a cookie HEADER - text instructions sent along with the HTML page from your script to the user's browser. The steps now become:

1. Check to see if there is a previously created COOKIE FILE. If so, put the items stored there in @purchases, otherwise leave @purchases empty.

2. Add the current purchase to @purchases.

3. Create a new COOKIE VARIABLE, $C_records, and store the value of @purchases there.

4. Print the new cookie variable as a COOKIE HEADER.

5. When the browser reads the COOKIE HEADER, it will create a new COOKIE FILE. This file will either replace the old previously created cookie file if it exists, or will be created as a completely new file.

Why should you check for a pre-existing COOKIE FILE before creating a new COOKIE VARIABLE? Because your script has no way of knowing whether this is the first item the user is purchasing or the tenth item. If it is the first item, checking for the COOKIE FILE will simply not find anything and the new COOKIE VARIABLE will be created entirely from the user's current purchases. If it is the second, or third, or tentch item the user is purchasing, checking for the COOKIE FILE will find the previous purchases and add them to the current purchase so that when you create a new COOKIE VARIABLE, that variable will contain both current and previous purchases.

  • Comment on Re^4: Question regarding CGI and cookies

Replies are listed 'Best First'.
Re^5: Question regarding CGI and cookies
by Anonymous Monk on Nov 11, 2004 at 23:21 UTC
    Thank you very much! Your answer makes a lot of sense. :)