Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookie problems

Status
Not open for further replies.

simanek

Programmer
Joined
Jan 19, 2001
Messages
137
Location
US
Hi all,

I have an odd problem with retrieving a cookie.

1. I set the cookie as such:
print "Set-cookie: cookieName=3; " . $HTMLHEADERS
The HTMLHEADERS var contains the Content-type: statement and other general formatting. I'm almost positive that it's being set correctly.

2. I retrieve the cookie from another page as such:
sub get_cookie{ #credit given to this forum and 'pump' for
#the routine
local($chip, $val);
foreach (split(/; /, $ENV{'HTTP_COOKIE'})){
#i'm using solaris so the env var should work fine
s/\+/ /g;
($chip, $val) = split(/=/, $_,2); #split on first '='
#now convert hex to ascii
$chip =~ s/%(A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
$val =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
# Associate key and value
$cookie{$chip} .= "\1" if (defined($cookie{$chip}));
# \1 is the multiple separator
$cookie{$chip} .= $val;
}
}

3. I print the value of the cookie as such:
print &quot;<P>Cookie Value: &quot; . $cookie{'cookieName'} . &quot;</P>&quot;;

4. And here comes the question:
The html page displayed shows the following
Cookie Value: 33

So where on earth did that box thing and the repeated value come from and how do I rid myself of it? Any help or thoughts would be greatly appreciated as I am at my wits end on this one. Thanks.


Mike
~~~~
simanek@uiuc.edu
&quot;It's a Swingline!&quot;
~~~~
 
One more thing: when I said that I'm on solaris, I mean that apache server is running on solaris. the client is ie5. Thanks. Mike
~~~~
simanek@uiuc.edu
&quot;It's a Swingline!&quot;
~~~~
 
Setting and retrieving cookie values is *MUCH* easier when done with CGI.pm. CGI.pm takes care of all the parsing for you - look how simple retrieving a cookie value is:

use CGI qw(:standard);
my $q = new CGI;
$abc = $q->param(&quot;abc&quot;);

The last statement retrieves the value of cookie &quot;abc&quot; into variable $abc. The downside is that you retrieve POST and GET variables the same way - using the &quot;param&quot; method, so you can't really tell how &quot;abc&quot; came into your program. But I think there's a way to tell, I just don't know what it is off the top of my head.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Yea, I know there is a module for it but I'm a firm believer in doing it once by yourself just to see how it works. Thanks for the code sample though, I'll keep track of that. For anyone who wants to know why I was having that problem, it turns out that I was inserting a newline between the cookie-set and the content-type http headers. Wow was that fun figuring out.

Mike
~~~~
simanek@uiuc.edu
&quot;It's a Swingline!&quot;
~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top