CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Cookies

 



PeterLove
Deleted

Jun 17, 2000, 5:11 PM

Post #1 of 6 (812 views)
Cookies Can't Post

Hello

What Im trying to do here is take the selected option and then make it a cookie for I can use it latter.
The problem is I can't get it to work. Smile

Here is my options code:
...
print "<td><select name=\"theoption\">";
print "<option value=\"none\">Options/Addons</option>";
for (keys %$options_hash_ref) {
my $prod_id = $_;
for (keys %{$options_hash_ref->{$prod_id}}) {
my $option_id = $_;
($descr, $optionprice) = @{$options_hash_ref->{$prod_id}{$option_id}};
@optarray = ($prod_id, $option_id, $descr, $optionprice);
if($prod_id eq $what->[0] &#0124; &#0124; $prod_id == $what->[0]) {
print "<option value='@optarray'>$descr</option>";
}
}
}
print "</select></td>";
...

Here is my code where I take the value from the selected field and make it a cookie.
...
elsif(param('do') eq "add") {
$id = param('id');
$qty = param('qty');
@opt = param('theoption');
%cart = &get_cookie_hash('cart');
$cart{$id} = $cart{$id} + $qty;
if($cart{$id} < 1) { delete $cart{$id}; }
$cookie = cookie(-name=>'cart',-path=>'/',-value=>\%cart);
$optcookie = cookie(-name=>'optioncookie',-path=>'/',-value=>@opt);
$loc = "http://$ENV{'SERVER_NAME'}/$cgi_dir/shopping.cgi?do=maincats";
my $list_ref = [$cookie, $optcookie];
print header(-COOKIE=>$list_ref,-LOCATION=>$loc);
}
...

Now Im trying to take the cookie value and make it an array:

@opt = &get_option_cookie('optioncookie');


Here is the get_option_cookie code:
sub get_option_cookie {
my($what) = shift;
my(@arraycookie);
@arraycookie = cookie($what);
return(@arraycookie);
}


I really need help here. Can someone please tell me what Im doing wrong and how to fix it?
(Code too please Smile )

Thanks

Jimmy


edesign
Deleted

Jun 19, 2000, 10:00 AM

Post #2 of 6 (812 views)
Re: Cookies [In reply to] Can't Post

Peter,

I would start by using the following code to write your cookie (assuming you have already parsed the form):

print "Set-Cookie:info_name=$formdata{'input_name'}\n";

You can then add things like the expiration date, etc. Then, to read your cookie and use the information, I would use this code:

if ($ENV{'HTTP_COOKIE'}) {
@cookies = split (/;/, $ENV{'HTTP_COOKIE'});
foreach $cookie (@cookies) {
($name, $value) = split (/=/, $cookie);
$data{$name} = $value;
}


$info_name = $data{'input_name'};
print "$info_name";
}
else {
print "Sorry! No cookies found";
}

I hope this helps! Post a reply if you need anything more.

-Daniel Smile

[This message has been edited by edesign (edited 06-19-2000).]


PeterLove
Deleted

Jun 19, 2000, 11:23 AM

Post #3 of 6 (812 views)
Re: Cookies [In reply to] Can't Post

Thanks for the help,

I have a few questions though. Smile

Where you have:
if ($ENV{'HTTP_COOKIE'})
I already have another cookie...
$cookie = cookie(-name=>'cart',-path=>'/',-value=>\%cart);

What would I change the if statement to, to direct to the right cookie?

And: print "Set-Cookie:info_name=$formdata{'input_name'}\n"; will make the option cookie?
Or do I use my old way I had made the cookie?
$optcookie = cookie(-name=>'optioncookie',-path=>'/',-value=>@opt);
(I don't know if that worked right though.) Smile


Thanks for you help, I appreciate it.
Peter


edesign
Deleted

Jun 19, 2000, 1:07 PM

Post #4 of 6 (812 views)
Re: Cookies [In reply to] Can't Post

Peter,

I am not familiar with the code you are using to create a cookie. Perhaps you are using a different form of CGI then my perl such as C++.

-Daniel Smile


PeterLove
Deleted

Jun 19, 2000, 1:15 PM

Post #5 of 6 (812 views)
Re: Cookies [In reply to] Can't Post

Its ok. Smile
But can you explain to me the questions I had in my last message? And a little on your way of making a cookie? Smile


Thanks man
Peter


edesign
Deleted

Jun 19, 2000, 2:17 PM

Post #6 of 6 (812 views)
Re: Cookies [In reply to] Can't Post

Peter,

I am not quite sure how to specify a specific cookie... although I am pretty sure you can only have one per domain. So, using the if statement to find the cookie should be fine, because it will find the cookie for your domain. I am not sure whether or not you can use your code to print the cookie because I have never done it that way...

-Daniel

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives