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

Getting data from a form

Status
Not open for further replies.

robb1975

Technical User
Apr 27, 2002
2
GB
Hi I'm new to perl and would like a few pointers on a perl script that I am writing. The script that I am writing has to retrieve data from a web form using either the 'Post' or 'Get' method. the data will be in the following form. username:password:group:sh*ll :superuserstatus. I then have to write this data to a text file I have got so far but can get no further. I have tried running the script from both the command line and through the form. But to no avail. The data is being passed using the get method as I can see it in the URL but the data is not being procesed.
I have also included the code thanks for any help.

Cheers
Robb

#! /usr/bin/perl
print "Content-type: text/html\n\n";
my $cgi_data;
my $no_of_data=0;

if (-e "Passwordfile.txt")
{
open(pass, ">>Passwordfile.txt");
}
else
{
open(pass, ">Passwordfile.txt");
}

if((($ENV{REQUEST_METHOD} eq "GET") and ($ENV{QUERY_STRING} ne "")) or
(($ENV{REQUEST_METHOD} eq "POST") and ($ENV{CONTENT_LENGTH} > 0)))
{
if($ENV{REQUEST_METHOD} eq "GET")
{
$cgi_data = $ENV{QUERY_STRING};
}
else
{
read(STDIN,$cgi_data,$ENV{CONTENT_LENGTH});
}
my %form_data_list;
@form_data_list = split(/&/,$cgi_data);
for (my $i=0; $i<@return_data; $i++)
{
my ($name, $value) = split(/=/,$return_data[$i]);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;,hex($1))/eg;

if (defined($form_data_list{$name}))
{
$form_data_list{$name} .=&quot;,$value&quot;;
} else {
$form_data_list{$name} = $value;
}
}
if($cgi_data =~ /[;><&\*`|[]/)
{
print &quot;You have breached Security&quot;;
exit 0;
}

$no_of_data=1;
}

if ($no_data == 0)
{
print (&quot;\n&quot;);
print (&quot;No data has been passed\n&quot;);
}
else
{
print (pass &quot;$value[0]:$value[1]:$value[2]:$value[3]:$value[4]&quot;);
}
close(pass);

 
You are making this much harder than it needs to be. There are two modules designed to handle CGI request in just this fashion. cgi-lib.pl and cgi.pm

The cgi.pm is built into PERL code so I would suggest using that one.

#!/usr/bin/perl
use CGI;
$query = new CGI;

$value = $query->param('name_of_parameter_passed');

print &quot;Now you can play with: $value&quot;;

HTH
 
Thanks. The only problem is that I am not allowed to use ant perl libaries or modules. Thanks anyway.
 
There are a few problems with you code. I have not tried to run it, so, I'm not sure I found all of them, but, this is a start.

#!/usr/local/bin/perl

if((($ENV{REQUEST_METHOD} eq &quot;GET&quot;) and ($ENV{QUERY_STRING} ne &quot;&quot;)) or
(($ENV{REQUEST_METHOD} eq &quot;POST&quot;) and ($ENV{CONTENT_LENGTH} > 0)))
{
if($ENV{REQUEST_METHOD} eq &quot;GET&quot;)
{
$cgi_data = $ENV{QUERY_STRING};
}
else
{
read(STDIN,$cgi_data,$ENV{CONTENT_LENGTH});
}
my %form_data_list;
@form_data_list = split(/&/,$cgi_data);

for (my $i=0; $i<@return_data; $i++)
{[red]# This 'for' loop will always fail the condition.
# Since @return_data does not exit yet, $i can never be
# less than the number of elements in @return_data. Consequently,
# this loop never actually does anything.
# maybe it should be &quot;for (my $i; $i<@form_data_list; $i++)&quot;
# If so, all references to 'return_data' need changing[/red]
my ($name, $value) = split(/=/,$return_data[$i]);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;,hex($1))/eg;
if (defined($form_data_list{$name}))
{
$form_data_list{$name} .=&quot;[red],[/red]$value&quot;;
} else {
$form_data_list{$name} = $value;
}
}
if($cgi_data =~ /[;><&\*`|[]/)
{
print &quot;You have breached Security&quot;;
exit 0;
}

$no_of_data=1;
}

if ($no_data == 0)
{
print (&quot;\n&quot;);
print (&quot;No data has been passed\n&quot;);
}
else
{
# The syntax you are using here refers to elements
# in an array named '@value'.
# $value[0] is the first (zero) element in the @value list.
# That array has not been created, thus, you won't
# see any elements.
print [red]qq[/red](pass &quot;$value[0]:$value[1]:$value[2]:$value[3]:$value[4]&quot;);

# Once the errors further up the code are fixed, this might work
foreach (keys(%form_data_list))
{
print qq(<p>KEY: $_ ; VALUE: $form_data_list{$_}</p>\n);
}
}
close(pass);

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top