Gossamer Forum
Home : General : Perl Programming :

Set Cookie

Quote Reply
Set Cookie
Why doesn't this code set the cookie properly? No cookie gets set with this code, and I can't understand why? Thanks for any input you guys can give me.

Code:
#######################################################################################
#######################################################################################

sub get_unique_id {

my $randlet = chr(65 + int(rand(26))) . chr(65 + int(rand(26)));
my $randnum = 100_000 + int rand 900_000;
my $randid = $randlet . $randnum;

&connect_to_db;

$sth = $dbh->prepare("SELECT id FROM bs_sessions WHERE id = $randid");
$sth->execute();

my $isnumrand = $sth->fetchrow_hashref;

$sth->finish();

if ($isnumrand->{id}) {
&get_unique_id;
}

return $randid;
}

#######################################################################################
#######################################################################################

sub set_cookie {

&get_unique_id;

$cookie_id = $query->cookie (
-name => 'id',
-value => $randid,
-expires => '+1w',
-path => '/',
-domain => 'fbagroup.co.uk'
);

print $query->header(-cookie=>[$cookie_id]);

}

- wil
Quote Reply
Re: [Wil] Set Cookie In reply to
Try:

Code:
sub set_cookie {

my $randid = &get_unique_id;

$cookie_id = $query->cookie ( -name => 'id',
-value => $randid,
-expires => '+1w',
-domain => '.fbagroup.co.uk'
);

print $query->header( -cookie => $cookie_id );

}

Last edited by:

PaulW: Nov 16, 2001, 7:21 AM
Quote Reply
Re: [PaulW] Set Cookie In reply to
But $randid should get returned to the sub anyway without assigning the variable again?

Wil

- wil
Quote Reply
Re: [Wil] Set Cookie In reply to
I prefer to keep things neat so you know what is coming from where, but that wasn't all I changed.

You need a . infront of the domain and [ ] wasn't needed - you only add that if setting multiple cookies.

Last edited by:

PaulW: Nov 16, 2001, 8:42 AM
Quote Reply
Re: [PaulW] Set Cookie In reply to
I don't need a period in front of the domain? The way it's set up the cookie shoukld follow you around for any sub domain of the domain fbagroup.co.uk?

Wil

- wil
Quote Reply
Re: [Wil] Set Cookie In reply to
>>The way it's set up the cookie shoukld follow you around for any sub domain of the domain fbagroup.co.uk?
<<

No it doesn't you need the dot.


Quote Reply
Re: [PaulW] Set Cookie In reply to
I'll give it a try.

Thanks

- wil
Quote Reply
Re: [Wil] Set Cookie In reply to
No, that didn't work :-\

- wil
Quote Reply
Re: [Wil] Set Cookie In reply to
Well the cookie code is ok
Quote Reply
Re: [PaulW] Set Cookie In reply to
The randid must not be generating properly. Not sure why. although in testing it does, and if I change $randid to "test" it still doesn't set the cookie.

Cheers

- wil
Quote Reply
Re: [Wil] Set Cookie In reply to
Hi -

I have experienced problems properly setting cookies in Netscape using the "domain" parameter, with or without the dot.

In the end, I simply removed the domain parameter which resolved the problem for me.

--
Matt G