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.


In reply to Re^4: Question regarding CGI and cookies by jZed
in thread Question regarding CGI and cookies by JOT007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.