Re: Question regarding CGI and cookies
by diotalevi (Canon) on Nov 08, 2004 at 21:23 UTC
|
I'd like to note that the textbook author's code is a poor example for code. Typically you'd want to declare your variables at the moment they become relevant. This limits the scope where a variable is visible and promotes the ability to refactor your code. | [reply] |
Re: Question regarding CGI and cookies
by xorl (Deacon) on Nov 08, 2004 at 21:18 UTC
|
I and other students do not understand the order of how this runs because it makes sense to us that you would gahter the information, create a cookie, send the cookie to the browser, then retrieve the cookie to print.
Why would you retrieve the cookie you just sent to the browser?
For the rest of the question, I think you are asking where the script gathers the info, creeates the cookie, and sends the cookie to the browser...
#retrieving cookie
@purchases = split(/ /, cookie('cookie'));
#adding purchase to cookie
push(@purchases, $basket);
#create cookie
$C_records = cookie(-name => "cookie",
-value => "@purchases",
-path => "/cgi-bin/c11ex6.cgi");
#send cookie to browser
print header(-cookie => $C_records);
This section adds the most recent purchase to the @purchases array, creates a cookie with all of the purchases, and then sends the cookie to the browser.
I don't really understand where you think the code shouldn't work. Did what I say help? If not can you clarify? | [reply] [d/l] |
|
|
We got the above code to work perfectly, but we are confused about the order the script performs the functions and we wish to understand it. The first function says we are retrieving the cookie, the second says we are adding to the cookie, the third says create the cookie, and the fourth says we are sending the cookie to the browser. To all of us, this does not make sense. How can it retrieve a cookie when it has not been created yet until further down? Does this help in what we are asking? Thank you....
| [reply] |
|
|
1. retrieve an old cookie containing *previous* purchases
2. add *current* purchases to the previous purchases
3. create a new cookie with the *combined* current and previous purchases
4. send the new cookie to the browser
...
PROFIT!!!
| [reply] |
|
|
|
|
Re: Question regarding CGI and cookies
by Cody Pendant (Prior) on Nov 08, 2004 at 23:44 UTC
|
What did you think was the "logical order" for this script? If we knew that then we might have more of an idea.
I'll take a guess though. You don't get in what order the getting and setting of cookies has to happen.
A page or script can get cookies from the browser as soon as the browser requests it.
BROWSER: Can I have the page "c11ex6.cgi" please?
SCRIPT: Certainly, just wait one second.
SCRIPT (privately) gets the previous cookie, reads the contents of $basket, adds it to @purchases, and constructs a new cookie with the changes incorporated
SCRIPT: OK browser, here you go. Here's an HTTP header containing an updated cookie, and here's the HTML of the page.
BROWSER: Thank you. (stores the new cookie)
Does that make sense in terms of what has to happen in what order? The old cookie has to be read first, the new cookie has to be sent out with the header, before anything else is sent, and next time the browser comes to this script, the script will read the cookie sent the last time.
I can remember finding this very confusing myself...
($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print
| [reply] |
|
|
We got the above code to work perfectly, but we are confused about the order the script performs the functions and we wish to understand it. The first function says we are retrieving the cookie, the second says we are adding to the cookie, the third says create the cookie, and the fourth says we are sending the cookie to the browser. To all of us, this does not make sense. How can it retrieve a cookie when it has not been created yet until further down? Does this help in what we are asking? Thank you....
| [reply] |
Re: Question regarding CGI and cookies
by Velaki (Chaplain) on Nov 08, 2004 at 21:09 UTC
|
It looks like you're missing your form code. The above simply prints out the contents of a submitted basket -- from another form on another page.
To add the form code, take a look at CGI.
Hope that helped,
-v
"Perl. There is no substitute."
| [reply] |
|
|
It is pulling info from a html doc...here is the code for it.
<!c11ex6.html>
<HTML>
<HEAD><TITLE>Wicker Baskets, Inc.</TITLE></HEAD>
<BODY bgcolor="#ffcccc">
<H1>Wicker Baskets Inc.</H1>
<HR>
<FORM ACTION="http://tinfoil.nsm.tridenttech.edu/cgi-bin/c11ex6.cgi" M
+ETHOD=POST>
<H2>Select a basket:</H2>
<P><INPUT TYPE=radio NAME=Basket Value=0 CHECKED>One Pint Square<BR>
<INPUT TYPE=radio NAME=Basket Value=1>One Quart Square<BR>
<INPUT TYPE=radio NAME=Basket Value=2>One Quart Oblong<BR>
<INPUT TYPE=radio NAME=Basket Value=3>Four Quart Square<BR>
<INPUT TYPE=radio NAME=Basket Value=4>Four Quart Oblong</P>
<P><INPUT TYPE=submit VALUE="Purchase This Basket"></P>
</FORM></BODY></HTML>
As you can see from the above html code, when the user clicks a radio button, it sends the data to a cgi script. This was an exercise in the book we had to do for a graded assignment, with very confusing examples to help us. Just wanting to understand the process the script makes the cookie form the code I submitted, because the process seesm backward unless perl just executes in the no particular order..... | [reply] [d/l] |
|
|
Thank you for the CGI link.....Though I still have not got the answer I was looking for, this site does have some helpful info and is better than our textbook....
| [reply] |
Re: Question regarding CGI and cookies
by Cody Pendant (Prior) on Nov 09, 2004 at 05:08 UTC
|
Can you tell us the book you're using? It doesn't seem to be one of the standards or someone would have recognised it.
If there's no suggestion at all the the person using this rather simple Shopping Cart script has bought something and set a cookie before, it could still make sense in a way.
It doesn't need a previous cookie to work. It doesn't say
#retrieving cookie
@purchases = split(/ /, cookie('cookie')) || die "No cookie!";
after all.
You'll just end up with an array with zero elements. Onto which will be pushed whatever you just bought.
Perhaps it should say
#retrieving cookie if there is one
at that point, if that's the intention?
Anyway. Why would you get a cookie, then turn around right away and set one?
This is an example of a shopping cart, which is a frequently-used metaphor in online shopping. If you go to Amazon or any other online shopping place, what happens? You click to buy a book, and, is the process over at that point? No. You're not asked for your credit card yet. You may choose other books, in the same way as you may use a real shopping cart and select a number of real-life items then go to the checkout.
The imaginary situation is that you've selected some items already, and by using the form, you've added another one to that list of items. Use the form again, and you add another item. Use the form a hundred times and you add a hundred items, and they are all recorded in the cookie.
The weirdness is, it's OK for you to have selected no items yet, the script is intended to work starting from zero items, or any higher number.
That's why you get and set. You get the cookie which says "he has already bought one thing", you add to it and send back a cookie saying "now he has bought two things" and so on.
($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print
| [reply] [d/l] [select] |
|
|
The text book we are using for this course is CGI/Perl by Diane Zar....It is a Course Technology book by Thomson Learning. This was an exercise in Chapter 11 on cookies and it was the last exercise in the chapter (6). Our instructor could not explain why the order works and he thought like we did, that it should be put in a logical step of creating the cookie, adding to the cookie, sending the cookie to the browser and retrieving the cookie. If we move the lines of script around into the order I just said, then the script will only show the last item clicked and not any previous items if there had been any others clicked before the browser was closed. The script that does work was from his answer sheet with the order that did not make sense to us or him, but it worked. We are just wanted to know how can it create a cookie long before the statement to do it is done? I know this is not a pratical application in real life, but just an assignment to teach us how to create a cookie and to add to it....
| [reply] |
Re: Question regarding CGI and cookies
by zentara (Cardinal) on Nov 09, 2004 at 13:04 UTC
|
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
| [reply] |
|
|
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....
| [reply] |
|
|
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?
| [reply] |
|
|
|
|