I have the following code. There is a subroutine named login. It logs in to a webpage with given credentials and grabs a cookie using LWP::Useragent. It would appear that the login subroutine is not storing the cookie in the useragent "cookie jar", since I am providing the script with a valid username and password, but I still get the "bad username or password error" from the script. This is the first time I have worked with subroutines, and I have had to move variables around to accomodate them, but I have screwed something up.
#!/usr/bin/perl use LWP::UserAgent; use Win32::GUI; $tmp = $ENV{"TEMP"}; my $font = Win32::GUI::Font->new( -name => "Comic Sans MS", -size => 12, ); $main=Win32::GUI::Window->new(-name=>"Main",-width => 400, -height => + 400); $main->AddButton(-name=>"BOK",-text=>"submit!",-pos=>[148,335], -onCl +ick=> sub {SUBMIT($single_entry_number, $user, $password)}); my $single_entry_number = $main->AddTextfield( -name => "single_entry_number", -text => "", -left => 10, -top => 10, -width => 200, -height => 25, -prompt => ["URL:", 80], ); my $user = $main->AddTextfield( -name => "user", -text => "", -left => 10, -top => 40, -width => 200, -height => 25, -prompt => ["Username:", 80], ); my $password = $main->AddTextfield( -name => "password", -text => "", -password => 1, -left => 10, -top => 65, -width => 200, -height => 25, -prompt => ["Password:", 80], ); $main->Show(); Win32::GUI::Dialog(); sub Window_Terminate { return -1; } sub SUBMIT { my $login = 1; my $ua = LWP::UserAgent->new(%uaoptions); $ua->cookie_jar( {} ); if ($login) { &login; } my $logintest= get("http://www.site.com/"); if ($logintest =~ m!you are now logged!sim) { } else { print "Bad username or password."; exit; } sub get { my $urls = shift @_; my $result = $ua->get($urls); if (!($result->is_success)) { die "Error: $urls produced " . $result->status_line; } return $result->content; } sub login { my $lpage = get("http://www.site.com/login.html"); my $chal; if ($lpage =~ /name=[\'\"]challenge[\'\"].*?value=[\'\"(.*?)\' +\"]/) { $chal = $1; } else { die "Couldn't find challenge in login.html"; } my %form = ( 'user', $user, 'password', $password, 'challange', $chal, 'action:log-in', "Log in..." ); my $result = $ua->post("http://www.site.com/login.html", \%form); } exit; }
In reply to Just between you, me and subroutines by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |