Gossamer Forum
Home : General : Perl Programming :

manipulating value from cookies

Quote Reply
manipulating value from cookies
I'm trying to write a script using Matt Wright's cookie library that saves the ID number of a link to a cookie. I've managed to save multiple ID numbers to the cookie by using a delimeter. To display the cookie, I split the records into an array, and pass each value through a loop to generate the link for that ID. My problem is I can't figure out how to delete an ID from the cookie.

I hope someone that understands Perl a bit more that I do can help me out...

--Drew
Quote Reply
Re: manipulating value from cookies In reply to
you're making something like mylinks?

well.. if you can put multiple ids into a cookie.. joined by a deliminator.. you should just split it by the deliminator..

Jerry Su
Quote Reply
Re: manipulating value from cookies In reply to
Yes, I'm trying to do something like MyLinks, only it will also grab news headlines and stuff.

I don't have any trouble adding stuff or displaying things -- that part I can do. Problem is when I try to remove an ID then rewrite the cookie, the value ends up empty. If I then try to add an ID, I get a null record and the one I just added (if that makes sense).

I thought that this would work:
Code:
@fav = split(/x/, $Cookies{'links'});
foreach $link (@fav) {
if ($link != $id) {
$new .= "$link";
$new .= "x";
}
}
chop ($new);
&SetCookies('links',$new);
but it doesn't (although when I ran this section as a standalone script, replacing $Cookies{'links'} with a sample string and &SetCookies... with print $new, the output looked correct).

--Drew
Quote Reply
Re: manipulating value from cookies In reply to
looks good to me.. maybe your "$Cookies{'links'}" is empty..

try printing it..

Jerry Su
Quote Reply
Re: manipulating value from cookies In reply to
Use CGI.pm instead, the cookie handling is much better. There are known bugs in matt's one.

See perldoc CGI for more info..

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: manipulating value from cookies In reply to
Okay I got this fixed... the null record was a result of bad logic in the add record routine. The logic was that if there was already a cookie, then just add a delimeter and the ID. But, if you delete the first record or all the records, and then add a new ID, the cookie would become corrupted (ie, the new cookie would be something like: "x10", with "x" being the delimeter). I'm not sure exactly what I changed to get the delete function to work correctly.

Thanks anyway Smile

--Drew
Quote Reply
Re: manipulating value from cookies In reply to
Thanks for the suggestion, Alex. I'll definately have look at that.

--Drew
Quote Reply
Re: manipulating value from cookies In reply to
yep.. mylinks.cgi was written using cgi.pm..

drew.. you do have mylinks.cgi don't you? you can copy it if you want..

Jerry Su
Quote Reply
Re: manipulating value from cookies In reply to
Okay, the CGI module works great. Now I need to set/read another cookie in the same script, so I made a copy of the original %cookies hash, so it looked like:
Code:
my %sources = (
name => 'sections',
expires => '+3y',
path => '/',
domain => 'ucmd.virtualave.net'
);
And in the subroutine where the cookie would be read, I added:
Code:
my $content = $in->cookie($sources{'name'});
So the entire sub looks like the following:
Code:
sub headlines {
my $in = shift;
my $content = $in->cookie($sources{'name'});

$us = &us_lines;
$w = &w_lines;
$b = &b_lines;
$t = &t_lines;
$h = &h_lines;
$s = &s_lines;
$cm = &cm_lines;
$ob = &ob_lines;

if ($content ne '') { #check for a cookie
@section = split(/x/, $content); #split the cookie up so we can read it
foreach $section (@section) { #don't display unwanted sections
$headlines .= qq|$us| unless ($section eq 'us');
$headlines .= qq|$w| unless ($section eq 'w');
$headlines .= qq|$b| unless ($section eq 'b');
$headlines .= qq|$t| unless ($section eq 't');
$headlines .= qq|$h| unless ($section eq 'h');
$headlines .= qq|$s| unless ($section eq 's');
$headlines .= qq|$cm| unless ($section eq 'cm');
$headlines .= qq|$ob| unless ($section eq 'ob');
}
}
else {
#display everything
$headlines .= "$us$w$b$t$h$s$cm$ob";
}

return ($headlines);
}
When I run the script, I get the following error:
Code:
Can't call method "cookie" on an undefined value at C:\HTTPD\CGI-BIN\music\portal.cgi line 225.
Which is the the "my $content" line above. If I comment the line out, the script proceeds as I expected it to when null or no cookie is found. What am I missing here? Unfortunately I only have LWP on my localhost but not on my server so I can't show you what it should look like when it's working.

--Drew
Quote Reply
Re: manipulating value from cookies In reply to
If I pull out everything related to the second cookie and save it as a another script, it works just fine... Is there a conflict between the two cookies or what? If you need to see the whole script I can post it as a .txt file.

--Drew
Quote Reply
Re: manipulating value from cookies In reply to
Doh!!! Create a new object, dummy Smile.

This:
Code:
my $in = shift;
my $content = $in->cookie($sources{'name'});
needed to be:
Code:
my $news = new CGI;
my $content = $news->cookie($sources{'name'});
--Drew