Perl passing var to sub

software development

#1

I wonder if anyone can help me out. I’m having great difficulties with my cookies. The following code doesn’t seem to want to set any cookies for me? Can anyone spot what’s wrong with the code? Thanks!

[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]);

}
[/code]The program calls the sub routine set_cookie; if no cookie exists, but it never seems to set it for some reason.

Am I passing the variable $randid onto the sub incorrectly?

Thanks fro any help anyone could give me.

Cheers

Wil


#2

There’s nothing wrong with the way you are calling the cookie method. However, if the value is undefined, the cookie will be undefined as well. So make sure $randid is not undefined. ie

if (defined $randid) { my $cookie_id = $query->cookie(<i>parameters</i>); print $query->header(-cookie=>[$cookie_id]); } else { print $query->header("text/plain"); print "Undefined \$randid\n"; exit; } Now, you may be wondering why $randid is undefined. That’s because in your set_cookie sub, you do not store the value returned from the call to get_unique_id.

sub set_cookie { if ($randid = &get_unique_id) { -- print header with cookie -- } else { -- print header without cookie -- }

  • until the end of the world -

#3

Hi

Thanks for your reply. I’ve now modified the code to:

[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;
}

if (!$randid) {
&error_html(“Random ID is empty.”);
}

return $randid;
}

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

sub set_cookie {

my $randid = &get_unique_id;

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

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

}

#######################################################################################
#######################################################################################
[/code]I didn’t know that I had to redifne [color=#CC0000]$randid[/color] in the second sub. I thought it got passed on automaticaly.

Anyway, still no joy I’m afraid.

I don’t get an error saying that randid is empty, but it still doesn’t want to set me a cookie.

If you get a minute, try the following URL:

http://www.fbagroup.co.uk/en/bookshop/index.cgi?pg=/en/bookshop/test.html

At the top of the page, there should be:

Session ID:

If the cookie has been set correctly, you should see a random number there as your session ID.

If not, theoreticaly it should return an error sub which it never seems to do.

Thanks

Wil


#4

Hey, Wil…

I went to the URL, and my cookie file now has this:

www.fbagroup.co.uk TRUE / FALSE 1006131164 id VM519808

…So apparently a cookie is being set… but maybe it’s not being read and/or the script just doesn’t know it’s been set.

I haven’t had a chance to look at the script; then again, I may not be able to tell what’s wrong even when I do…

…Bob W.


#5

Hi Bob

Thanks for checking the URL.

I think I’ve screwed up my cookies by deleting the cookie and not dereferencing it from the database. A quick reinstall might do the trick.

Cheers

Wil