1

I need to design a shopping cart using perl such that the user gets clear idea of the goods he choose to buy. I thought of saving data of those items in a cookie. But i wonder how to update an already existing cookie each time an item is added. Is there any better way to design a cart/checkout page. Is there any perl module which makes my work easier?

Here is the snippet i tried out for updating cookie of cart

$cooki = $q->cookie('CART'); #retrieve cookie CART if already exists into var $cooki
$val2 = $cooki;
$val1 = $picid;
$cooki=$q->cookie(-name=>'CART',
-value =>["$val1"," $val2"],
-expires=>'+5m',
-path=>'/');
print $q->header(-cookie=>$cooki);

retrieval:

$cooki = $q->cookie('CART');

But it stores only the current id of the pic selected like for ex '45%20' i.e. 45 with a space and not multiple values like '45 12 16' . Where could i go wrong?

navin
  • 47
  • 1
  • 9
  • I tried your code snippet and it works fine for me, please double check the values in the variables. – Pradeep May 16 '14 at 14:03
  • i couldn't get it. every time i add an item or call the script the value of the cookie is the id pertained only to that item. – navin May 16 '14 at 14:28
  • 1
    Storing anything except session ID in cookie is rarely good idea. – mpapec May 16 '14 at 15:42

3 Answers3

1

Do not use Cookies, instead use CGI::Session. For all the reasons why, you can read CGI::Session::Tutorial.

I created a working examples of using sessions to transfer information from one form to another in this question: How to access variable of other perl program in my perl program

Community
  • 1
  • 1
Miller
  • 34,962
  • 4
  • 39
  • 60
0

Try this, because the return is an array:

@cooki = $q->cookie('CART');
Pradeep
  • 3,093
  • 17
  • 21
  • the return is indeed an array. I modified it. But the retrieval displays for ex '25&0' i.e. the already existing cookie value is set to 0 or returning 0. – navin May 16 '14 at 14:55
0

Use CGI::Cookie and set cookies in anonymous array

 my $c = CGI::Cookie->new(-name    =>  'CART',
                          -value   =>  ['45','12','16'],

then fetch existing cookies by:

    %cookies = CGI::Cookie->fetch;
    $cart = $cookies{'CART'}->value;
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133