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

help with HTTP::Request::Form

Status
Not open for further replies.

gorgor

Programmer
Aug 15, 2002
164
US
Examples for this perl module is almost non-existent except for the ONE example in the documentation. I'm hoping somebody here has experience with this module. I'm using the following CGI script to submit a form I made (for practice). It doesn't seem to be 'submitting' the form. The result of this script doesn't seem to be anything more than a printout of my form page (not submitted). Could anyone tell me what is wrong here??? Feel free to give the script a try.

#-------------------------------------
#!c:\perl -w
use strict;

use CGI::Carp qw(fatalsToBrowser);

use URI::URL;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use HTTP::Request::Form;
use HTML::TreeBuilder 3.0;

my $ua = LWP::UserAgent->new;
my $url = url 'my $res = $ua->request(GET $url);
my $tree = HTML::TreeBuilder->new;
$tree->parse($res->content);
$tree->eof();

my @forms = $tree->find_by_tag_name('form');
die "What, no forms in $url?" unless @forms;
my $f = HTTP::Request::Form->new($forms[0], $url);
$f->field("username" , "user");
$f->field("password" , "pass");
$f->field("page","2");
my $response = $ua->request($f->press("submit"));
print $response->content if $response->is_success;

exit;
#-------------------------------


Just for reference, here is the 'form' that I am trying to submit with the above script. It is actually a Perl CGI so you can see what is required to get a valid submission. HTTP_REFERER must be from my domain and QUERY_STRING must be blank. A cookie is set once there is a valid form submission.

#-------------------------
#!c:\perl -w

use strict;
use CGI;
use CGI::Cookie;
use CGI::Carp qw(fatalsToBrowser);

my $query = new CGI;

#-------------- Global Variables ----------------

my $DOMAIN = ".globalnamespace.com";

my $username = $query->param('username') || '';
my $password = $query->param('password') || '';
my $page = $query->param('page');

#------------------------------------------------

#--------------- Main Program -------------------

if ($page eq "")
{
displayLoginPage();
exit;
}
elsif ($page eq "2")
{
if(!(checkReferer()))
{
print "Content-type: text/html\n\n";
print &quot;Breakin detected!!!<br>\n&quot;;
print qq{
page = $page<br>
username = $username<br>
password = $password<br>
referer = $ENV{'HTTP_REFERER'}<br>
query string = $ENV{'QUERY_STRING'}<br>
};

displayLoginPage();
exit;
}

if(!(verifyUser()))
{
print &quot;Content-type: text/html\n\n&quot;;
print &quot;That didn\'t work!<br>\n&quot;;
print qq{
page = $page<br>
username = $username<br>
password = $password<br>
referer = $ENV{'HTTP_REFERER'}<br>
query string = $ENV{'QUERY_STRING'}<br>
};

exit;
}
else
{
setVerCookie();

continuePage();
exit;
}

exit;
}
else
{
exit;
}

#------------------------------------------------


sub checkReferer
{
my $params =lc($ENV{'QUERY_STRING'});
$ENV{'HTTP_REFERER'} =~ s|.*(globalnamespace.com).*||i;

if(($params eq '')&&($1 ne ''))
{
return(1);
}
else
{
return(0);
}
}


sub verifyUser
{
if(($username eq 'user')&&($password eq 'pass'))
{
return(1);
}
else
{
return(0);
}
}

sub setVerCookie
{
## Set a Domain Cookie with no expiration date - expires on browser close
print qq{Set-Cookie: mycookie=set; domain=$DOMAIN; path=/;\n};
}

sub displayLoginPage
{

print qq
{
<html>
<head>
<title>Login</title>
</head>

<body>
<center>

**Please ensure that Javascript and Cookies are enabled**<br><br>
Please enter your Username and Password to Login:<br>
<br>
<form action=&quot;$ENV{'SCRIPT_NAME'}&quot; method=&quot;post&quot;>
Username: <font face=&quot;courier&quot; size='1'><input type=&quot;text&quot; size=10 name=&quot;username&quot;></font>
<br><br>
Password: <font face=&quot;courier&quot; size='1'><input type=&quot;password&quot; size=10 name=&quot;password&quot;></font>
</font>
<br><br>
<input type=&quot;hidden&quot; name=&quot;page&quot; value=&quot;2&quot;>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot; Login &quot;>
</form>

page = $page<br>
username = $username<br>
password = $password<br>
referer = $ENV{'HTTP_REFERER'}<br>
query string = $ENV{'QUERY_STRING'}<br>
</center>
</body>
</html>
};
}


sub continuePage
{

print qq{
<html>
<head>
<title>Welcome!</title>
<body>
<center>
<br>
<a href=&quot;enter.html&quot;>Continue...</a>
<br>
<br>

</center>
</body>
</html>
};
}
#-----------------------------------
 
disclaimer: I have never used this module before and I know almost nothing about it, so I can't really help you beyond what I'm about to point out.

The docs for the module say that the field() method doesn't work for hidden fields. You, however, are using it to fill in the value for &quot;page&quot;, which is a hidden field according to the form you posted. Perhaps remove that line and try it again.

Good luck.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top