everlast88az has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am a total newb at this but learning perl seems fun.. I am trying to login to a website the script appears to be successffull but returns strange output that is listed below it returns a script rather than a webpage?? Is there a way to go to a specific page after logging in? I want to go to the actual page you are redirected to after login. Here is my script and the output is below
use WWW::Mechanize; use HTTP::Cookies; my $outfile = "out.htm"; my $url = "***************"; my $username = "**********"; my $password = "********"; my $mech = WWW::Mechanize->new(); my $number = 2; $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url); my @names = $mech->forms(); #print @names; #mech->form_id( $name ); $mech->form_name('login_form'); #$mech->form_number($number); $mech->field(USER => $username); $mech->field(PASSWORD => $password); $mech->submit(); my $output_page = $mech->content(); print $output_page; @links = $mech->find_all_links(); foreach (@links) { print $_->url(), "\n"; print $_->text(), "\n"; print $_->name(), "\n"; print $_->tag(), "\n"; } This gives::: <!-- --> <script language="Javascript"> loc = document.URL; begin_target = loc.indexOf("TARGET="); end_target = loc.length; target = loc.substring(begin_target + 7,end_target); target = target.replace(":80",""); target = target.replace('$SM$', ''); end_site = target.indexOf('/',10); site = target.substring(0,end_site); if(end_site == -1) { end_site = target.indexOf('%2f',16); } site = target.substring(0,end_site); while( (site.indexOf('%3a')) != -1) { site = site.replace('%3a', ':'); } while( (site.indexOf('%2f')) != -1) { site = site.replace('%2f', '/'); } while( (site.indexOf('%2e')) != -1) { site = site.replace('%2e', '.'); } document.location.href = site + "/?DEST=" + target; </script>
Which is not what I want. Any ideas why I am getting this rather than an html page??

Replies are listed 'Best First'.
Re: form logins
by bilfurd (Hermit) on Apr 06, 2009 at 01:58 UTC
    Howdy, everlast88az,

    Without going into the code you posted, my first suggestion is to check your web server settings to make sure that you are posting your code to the proper cgi directory. (This varies from platform to platform.)

    Be sure to edit your code to "use strict;" before somebody notices. It is good habit to get into, and one that Perl Monks will bring to your attention every time.

    Good luck!

    - Bilfurd

      this reads from a website I do not have direct control over the directory, it doesn't even use cgi. And what good is the strict if I am not using any functions or anything that needs local variables..
        The value of strict.pm for me is that it catches most of my typos. This is true even in short pieces of code which don't need any functions.
Re: form logins
by moritz (Cardinal) on Apr 06, 2009 at 06:32 UTC
    Which is not what I want. Any ideas why I am getting this rather than an html page??

    You're getting this because that's what the server sends to you. The server also sends that to the browser, which interprets the javascript and redirects you to another. This step happens transparent to you, you probably never noticed.

    The easiest way is to solve your problem is to emulate the javascript code in Perl, and run it when you receive that page, and then do another request with the URL in document.location.href.

      This is correct... In fact this is a commen thing nowadays where the page that is visited is just adding their own content to a existing page and passing it to the end user...
      another trick in use today is to add conditions that the end user will only get the correct page on the third or fourth time... so that redirection to advertisement are increased.


      Hacker_J99
      UTC File Server for all your File needs