Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to check if successfully logged in?

by Hellena (Initiate)
on Mar 12, 2013 at 14:37 UTC ( #1022988=perlquestion: print w/ replies, xml ) Need Help??
Hellena has asked for the wisdom of the Perl Monks concerning the following question:

I used WWW::Mechanize and script works, but I need to print whether it was successful or unsuccessful,how could I do that? Here is the script:
my $url = "http://www.kapija.org"; my $username = "User123"; my $password = "Password123"; my $mech = WWW::Mechanize->new(autocheck => 1); $mech->get($url); $mech->form_name('form1'); $mech->field(username => $username); $mech->field(password => $password); $mech->click();

Comment on How to check if successfully logged in?
Download Code
Re: How to check if successfully logged in?
by toolic (Chancellor) on Mar 12, 2013 at 14:41 UTC
      STATUS METHODS always back true statement..
Re: How to check if successfully logged in?
by kennethk (Monsignor) on Mar 12, 2013 at 14:44 UTC

    In addition to toolic's suggestion, consider how you can tell you've successfully logged in when you do it manually. Then read my sig.

    HTML::Parser may be helpful for this approach. Or Mojo::DOM.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: How to check if successfully logged in?
by Anonymous Monk on Mar 12, 2013 at 15:00 UTC
    Cross post at stack overflow, why do you want to automate a login to a board of site defacing script kiddies? Google search "hacked by kapija.org" "defaced by kapija.org".
      Script kiddies don't make script thay just use it ;) I trying to make security scanner for SQLI, I never deface someone site if he don't deserves it.
        And who are you to judge if someone "deserves" it? You can't even code your own basic perl scripts! Not something we want round here.
Re: How to check if successfully logged in?
by sundialsvc4 (Abbot) on Mar 12, 2013 at 17:05 UTC

    Every real-world mechanize script has to, first and foremost, make no assumption as to which screen actually will pop-up next.   (It’s not enough merely to check for HTTP 202.)   You have to identify the screen:   did “The Monastery Gates” really appear?   And so on.   The next follow-on question is to be sure that you are (still) logged in ... a peculiarly frequent problem with this particular site, sometimes.   You do this by explicitly checking, every time, that you do indeed find text like “Log my_userid Out” at the expected spot, vs. “Log In” at that same spot.

    A mechanize-script in actual production is not quite as simple as it first appears:   it must be a finite-state machine (FSM) design, because in the final analysis the host web-site is driving the bus.   Your logic must send the HTTP messages that you expect will work, but you always have to reconcile this with what actually comes back.   “Kilroy was an optimist” sometimes.

    Fact of the matter is, a production mechanize-script is often two FSMs:   one which tracks the state of the host, and the second which tracks the state of what you are trying to do.

      "The next follow-on question is to be sure that you are (still) logged in ... a peculiarly frequent problem with this particular site, sometimes. You do this by explicitly checking, every time, that you do indeed find text like “Log my_userid Out” at the expected spot, vs. “Log In” at that same spot. "

      Are you suggesting that this site has logged you out? In years of using the site I've never experienced this.

        Are you suggesting that this site has logged you out? In years of using the site I've never experienced this.

        sure you have, when you click a link to a different domain -- weak nit to pick, of all the piles of nonsense created by sundialsvc4

Re: How to check if successfully logged in?
by hwnd (Initiate) on Mar 12, 2013 at 21:29 UTC
    Why not just create a database to store your user id and passwords for the page/site and use DBI and Server Side Sessions to verify. Something like below:
    sub authenticate { my $username = shift; my $password = shift; return 0 unless $username && $password; my $dbh = DBI->connect("DBI:mysql:[DATABASE NAME]:[HOSTNAME]" "*****", "*****", { RaiseError => 1 }) or die + $DBI::errstr; my $sth = $dbh->prepare("SELECT id from table WHERE name = ? AND pass = ? LIMIT 1"); $sth->execute($username, $password); my ( $id ) = $sth->fetchrow_array(); $dbh->disconnect; return ($id) ? 1 : 0 }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022988]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2014-12-22 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (132 votes), past polls