#!/usr/bin/perl # Please note that this code is © Geoff Youngs, 2001 and is not to be # distributed with permission - it is tutorial purposes and although it # should be fully functional, it has not been used in a live environment. # Hence it is supplied with no guarantee (actual or implied) of being # suitable for any purpose whatsoever. use strict; # %COOKIE is a global hash which contains all relevant cookie values my %COOKIE = undef; # Usage: &decode_cookies(); # e.g. &decode_cookies; # $cart_id = $COOKIE{'cart_id'}; sub decode_cookies { my @pairs = split(/;\ /, $ENV{'HTTP_COOKIE'}); # '; ' is the divider foreach my $pair (@pairs) { $pair =~ tr/+/ /; (my $name, my $value) = split(/=/, $pair); $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if (!$COOKIE{$name}) { $COOKIE{$name}=$value; } else { $COOKIE{$name}.=", $value"; } } } # Usage: $data = &encode_data($rawdata); # e.g. print "Set-Cookie: ".&encode_data("My Cookie1")."=". # &encode_data("Some value for My Cookie1")."\r\n"; sub encode_data { my $data = shift; $data =~ s/([^-a-zA-Z:\/ ])/sprintf("%%%02X",ord($1))/eg; $data =~ tr/ /+/; return $data; }