in reply to Question regarding CGI and cookies

This isn't an answer to your cookie problem, as much as it is a comment on your web-store design. You are better off not using cookies, instead store the information in a db of some kind on the server. People can refuse cookies, change cookies,and otherwise mess up your store.

I guess if you are just trying to learn what cookies are, it's OK, but your store will be better in the long run, if you don't rely on them. Its is a good topic for discussion in your class.... cookies..good or bad?

For instance, cookies might be useful to temporarily store an unfinished order for a day or two. But still store the actual order in a db, and send an "encrypted cookie" which just holds the key to the saved db entry.


I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: Question regarding CGI and cookies
by JOT007 (Novice) on Nov 09, 2004 at 20:32 UTC
    Thank you for your input. This was just an exercise of a fictional store and how a cookie could be used to keep track of what was purchased. It was just an exercise to show us how to create a cookie and put in into an array that could be used again by the cookie to display what had been clicked on. I still do not understand how the script can run in the order it is and produce a cookie to display. I guess my question should have been, does perl run script line by line or does it know to bounce among the lines to execute without giving it sub-routines. We already learned sub functions in the last chapter... Can anyone explain why the above script works the way it does with it not being in logical order? Thank you again....
      Hi folks,

      The assignment only involves introducing the student to using cookies and a session cookie is used in the exercise, though the student could choose to add an expiration if they desired.

      The html code was not included because it was not necessary. The form only passes one variable via post from a selection of items displayed with a radio button, so only one item can be selected at a time. The user then uses their back button to select another item and submit it to the script.

      I know there are better ways to write the script but this is only to be a basic exercise.

      If you look at the code above, the order events is:

      1. declare variables
      2. assign input item to variable
      3. retrieving cookie
      4. adding purchase to cookie
      5. create cookie

      Now, logically, it would make sense that you would create the cookie first before retrieving it. But, if we put the code to the create cookie before the code to retrieve the cookie, the script only displays the item currently selected from the form. If we use the code as is, the item selected is kept in a session cookie and is printed out as well as other items selected when we use the back button to select another item.

      So, the question is, when a person uses the form for the very first time, there is no session cookie because it hasn't been created. Yet the code only works if we "retrieve" the cookie and then create it.

      Any ideas as to why it works this way?

        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.