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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from form, rock solid code now not working 1

Status
Not open for further replies.

Nutthick

MIS
Jan 14, 2004
126
GB
I've always used this code to read in data from my forms, but for some reason it's not working. The server is still fine and is working with other Perl scripts, it's just this one. Can anyone see what is wrong with it?

#!/usr/bin/perl

use CGI qw:)standard);

print header();

# Read in the form data
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

#$myKey = uc(trim($FORM{'Key'}));
$myKey = $FORM{'Key'};
print "Your Key $myKey\n";

#String to match
$keyMatch = "abc001";

if ($myKey eq $keyMatch)
{
# Send OK
print "This key is OK\n\n";
}
else
{
# Send Bad
print "This key is bad $myKey $keyMatch\n\n";
}

exit(0);

# Removes leading/trailing spaces
sub trim {
my @out = @_;
for (@out)
{
s/^\s+//;
s/\s+$//;
}
return wantarray ? @out : $out[0];
}
 
no syntax errors, bit unusual code but the script should work. Any changes to the server recently? Have you checked the server error logs?
 
Everything is looking fine. No errors logged on the server. I've even tried pointing a good piece of html at the script to see if it prints out the variables, but nothing. The script is working as it generates an output, but it's just never reading in the variables.

n.b. Code looks screwy as I've been messing with it for the last 2 hours, and stopped caring what it looked like
 
I've just cut it down to this, but still nothing

#!/usr/bin/perl

use CGI qw:)standard);

print header();

# Read in the form data
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
 
Cracked it, I changed the code to

#!/usr/bin/perl

print <<HEAD;
Content-type: text/html\n\n
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
HEAD

# Read in the form data
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

print "$FORM{'Key'}";

print <<FOOTER;
</body>
</html>
FOOTER


One more question, I'm trying to call the script by entering the url as follows


but it doesn't work. Any idea why?
 
Your code is processing the variables from stdin.
Your supplying a variable in the url instead of stdin.
You wonder why your code does not see the variable.
If you had used the "CGI" module it would handle all that processing for you and you could use access any of these variables or "parameters" as they are more commonly know via the "param" function.


Trojan.
 
Nutthick

I expect you have already sussed it - but your code is wanting POST data - and the URL with the appended ?Key=abc123 is GET data

I'm sure you are aware of the $ENV{QUERY_STRING} method...


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top