use CGI; # use the CGI as the default output object $query = new CGI; # get the input from the CGI object ### Get the current cookie and referring URL ### $cookie = $ENV{'HTTP_COOKIE'}; $ref = $ENV{'HTTP_REFERER'}; ### Get the item and quantity that the user added to their basket ### $item = $query->param('item'); $qty = $query->param('qty'); $button = $query->param('button'); ### If the item being added is not in the cookie, add it ### if ($cookie !~ $item) { $newcookie= $cookie . "," . $qty . "-" . $item; } ### If the item being added is already in the cookie, find it and update the quantity ### else { $itempos = index($cookie,$item); $uptoitem = substr($cookie,0,$itempos); $cookieqty = substr($cookie,(rindex($uptoitem,",")+1),($itempos-1)-(rindex($uptoitem,",")+1)); ### If this is a shopping basket update, replace the old quantity with the new quantity, otherwise, add the new quantity ### if ($button eq "Update") { substr($cookie,(rindex($uptoitem,",")+1),($itempos-1)-(rindex($uptoitem,",")+1)) = $qty; } else { substr($cookie,(rindex($uptoitem,",")+1),($itempos-1)-(rindex($uptoitem,",")+1)) = $cookieqty+$qty; } $newcookie=$cookie; } ### Return user to the same page and set the new cookie ### print "Location: http://www.authorservicesinc.com/cgi/cart.pl\n"; print "Set-Cookie: $newcookie; path=/; domain=www.authorservicesinc.com\n\n";