如何使用Perl在URL中传递变量?

时间:2013-05-16 18:10:07

标签: perl url variables foreach

我正在尝试在网址中传递参数。我不知道缺少什么,我试着看看执行这个脚本后URL的样子。

my $request3 = HTTP::Request->new(GET => $sql_activation);

my $useragent = LWP::UserAgent->new();
$useragent->timeout(10);

my $response2 = $useragent->request($request3);
if ($response2->is_success) {

    my $res2 = $response2->content;

    if ($res =~ m/[#](.*):(.*)[#]/g) {
        my ($key, $username) = ($1, $2);
        print "[+] $username:$key \n\n";
    }
    else {
      print "[-] Error \n\n";
    }
}

my $link =
      "http://localhost/wordpress/wp-login.php?action=rp&key=" 
    . $key
    . "&login="
    . $username;

sub post_url {

    my ($link, $formref) = @_;

    my $ua = new LWP::UserAgent(timeout => 300);
    $ua->agent('perlproc/1.0');
    my $get = $ua->post($link, $formref);

    if ($get->is_success) {
        print "worked \n";
    }
    else {
        print "Failed \n";
    }
}

执行脚本后,URL就像这样

site/wordpress/wp-login.php?action=rp&key=&login=

1 个答案:

答案 0 :(得分:1)

Perl具有块级范围。您可以在$key语句后面的块中定义$usernameif。他们不会活在那之外。

您需要在该块之前创建它们(使用my

# HERE
my ( $key, $username );
if ( $response2->is_success ) {
    my $res2 = $response2->content;
    if ( $res =~ m/[#](.*):(.*)[#]/g ) {
        # Don't say my again
        ( $key, $username ) = ( $1, $2 );
    }
    else { print "[-] Error \n\n"; }
}