Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How do I end a while loop early?

by Anonymous Monk
on May 07, 2013 at 23:28 UTC ( #1032542=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a login script and run into an error, first heres the code:

use DBI; use CGI qw /:standard/; use CGI::Cookie; use warnings; while (! defined $cookies{'authorized'}) { if (param) { my $username = param('username'); my $password = param('password'); my $dbh = DBI->connect("dbi:SQLite:dbname=/path/to/dat +abase.db","",""); my $sth = $dbh->prepare('select * from useers where username = ? and password = ?' +); $sth->execute($username,$password); my @row = $sth->fetch_array; if (@row) { #login success, set authorized cookie my $cookie = CGI::Cookie->new( -name=>'authorized', -value=>1, -path=>'/default/url'); last; ##THIS IS WHERE I NEED TO + END IT } } print header,start_html('Login'),h1('Login'), start_form, "Username: ",textfield('username'),br, "Password: ",password_field('password'),br, submit("Enter"); end_form, end_html; #######this generates an infinite loop of login forms } #once that specific cookie is set, i redirect print redirect('search.cgi');

So what's happening is

while (! defined $cookies{'authorized'}) { if (param) { if authorized { set cookie } #end here!!!!!!!!!!!!!!!!!! } #if we make it here, user isnt validated, so generate form generate login form } #now that we're out of the loop, redirect redirect

Any way to exit out of the loop?

Comment on How do I end a while loop early?
Select or Download Code
Replies are listed 'Best First'.
Re: How do I end a while loop early?
by ig (Vicar) on May 08, 2013 at 07:57 UTC

    I would try:

    use DBI; use CGI qw /:standard/; use CGI::Cookie; use warnings; if (! defined $cookies{'authorized'}) { if (param) { my $username = param('username'); my $password = param('password'); my $dbh = DBI->connect("dbi:SQLite:dbname=/path/to/dat +abase.db","",""); my $sth = $dbh->prepare('select * from useers where username = ? and password = ?' +); $sth->execute($username,$password); my @row = $sth->fetch_array; if (@row) { #login success, set authorized cookie my $cookie = CGI::Cookie->new( -name=>'authorized', -value=>1, -path=>'/default/url'); } else { print_login_form(); exit; } } else { print_login_form(); exit; } } #once that specific cookie is set, i redirect print redirect('search.cgi'); exit; sub print_login_form { print header,start_html('Login'),h1('Login'), start_form, "Username: ",textfield('username'),br, "Password: ",password_field('password'),br, submit("Enter"); end_form, end_html; }
Re: How do I end a while loop early?
by LanX (Canon) on May 07, 2013 at 23:50 UTC
    You can exit from a while loop early with last, but I really don't understand what you are doing there with a while-loop...

    Cheers Rolf

    ( addicted to the Perl Programming Language)

        > He's trying to continue prompting for a login until the user is authorized

        Within a CGI with a while loop???

        Does CGI.pm implement continuations turning HTTP state-machines inside-out?

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: How do I end a while loop early?
by choroba (Canon) on May 07, 2013 at 23:48 UTC
    See last.

    Oops. You already did.

    Update: Are you sure your logic is correct? Does the authorization really happen while generating the page?

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (14)
As of 2015-07-30 21:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls