Welcome to WebmasterWorld Guest from 54.196.244.186

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Dereferencing problems

with pointers to vars and arrays

     
5:04 pm on Aug 31, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


I am trying to check for the existence of server environment variables and then sanitize them and copy them into local variables if they exist. The following code works fine:

$referer = "-";
if (exists($ENV{'HTTP_REFERER'})) {
$string = $ENV{'HTTP_REFERER'};
&sanitize;
$referer = $string;
}

$usragnt = "-";
if (exists($ENV{'HTTP_USER_AGENT'})) {
$string = $ENV{'HTTP_USER_AGENT'};
&sanitize;
$usragnt = $string;
}


However, I have a fairly long list of such variables, and I'd like to use a subroutine instead of hard-coding for each server environment variable. The following code doesn't work -- I guess because I'm confused about the $ENV array pointers and symbolic references. It seems that the environment variable array pointer isn't de-referenced properly, and that the symbolic reference to the named program variable doesn't work either.

&sanivar ("\'HTTP_REFERER\'", $referer, "-");
&sanivar ("\'HTTP_USER_AGENT\'", $usragnt, "-");

# If the server environment variable exists, sanitize it and copy it to the named program variable.
sub sanivar {
local($string);
my($senvar, $locvar, $defval) = @_;
$string = $defval;
if (exists($ENV{$senvar})) {
$string = $ENV{$senvar};
&sanitize;
}
$$locvar = $string;
}


'$string' always ends up holding the default string value specified in the calling parameters list, and the 'exists' check apparently always fails.

Also, the local variable pointed to by symbolic reference '$$locvar' is not updated.

Any suggestion or corrections to this code, or suggestions on a better implementation would be most welcome.

Thanks!
Jim

10:24 am on Sept 1, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10544
votes: 8


i think you need to pass the dereferenced variable and you don't need the escaped single quotes.
try this:
&sanivar ("HTTP_REFERER", \$referer, "-");
&sanivar ("HTTP_USER_AGENT", \$usragnt, "-");
3:06 pm on Sept 16, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


The problem comes in, though, that if the environment variable doesn't exist (i.e. is undefined), then either PERL gives it a (defined) value of NULL or throws an error, depending on the PERL warning level.

I ended up just using:


$referer = &sanivar($ENV{'HTTP_REFERER'} "-");

and modifying the subroutine to suit. If the variable isn't defined or is NULL, then the 'OR' function kicks in and assigns it a value of "-".

If I get some more time, I'll go back and try your approach, though -- I hate leaving 'mysteries' unsolved just because I run out of time on a project...

Thanks,
Jim

Note to readers: Replace the broken pipe "" characters with solid pipe characters; Posting on this forum modifies the pipe characters.

8:29 pm on Sept 28, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 0


I think phranque's first post is right on track, the single quotes were probably the biggest problem.

btw: you don't need the & in front of subroutine-calls, iirc, there was even something possibly bad with using them, but I don't quite remember what it was.

also, when using ¦¦, you're checking for three things: an empty string, a zero-value or an undefined value. that might, in this case, be just fine for you, but it's not always what you'd want, so watch out.