I am running a login script and perhaps there is a better way to do this but I think I have it right. Here's how the program should run

fetch cookies check if authorized if yes, redirect if no, check for params if param check for credentials if credentials are good set authorized cookie redirect to default page else authorization failed display form to login

I feel this should work but I need to rerun the 'if param' if statement with the newly entered credentials to get this to work. Here is my code if its easier to look at that.

use DBI; use CGI qw /:standard/; use CGI::Cookie; use warnings; my %cookies = CGI::Cookie->fetch; if (defined $cookies{'authorized'}){ #redirect to search.cgi print redirect("search.cgi"); } if (param){ my $username = param('username'); my $password = param('password'); my $dbh = DBI->connect("dbi:SQLite:dbname=/var/tmp/database.db +","",""); my $sth = $dbh->prepare("select * from users where username = ? and password = ?"); $sth->execute($username, $password); my @row = $sth->fetch_array; if (@row){ #login successful # set 'authorized' cookie my $cookie = CGI::Cookie->new( -name=>'authorized', -value=>1, -path=>'/~default/chinook'); #-expires=>'+10m'); #redirect to search.cgi print redirect(-uri=>'search.cgi', -cookie=>$cookie); }else{ # login failed } } print header, start_html('Login'),h1('Login'), start_form, "Username: ",textfield('username'),br, "Password: ",password_field('password'),br, submit('Enter'), end_form, "\n";

Any ideas how to rerun the if(param) statement?

Maybe a subroutine?



Got it running, thanks to anyone who helped. Working code follows.

use DBI; use CGI qw /:standard/; use CGI::Cookie; use warnings; #print header('text/plain'); my %cookies = CGI::Cookie->fetch; 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 users where username = ? and password = ?" +); $sth->execute($username,$password); my @row = $sth->fetchrow_array; if (@row) { #login successful, set authorized cookie my $cookie = CGI::Cookie->new( -name=>'authorized', -value=>1, -path=>'/default/path'); #-expires=>'+10m'); print redirect(-uri=>'search.cgi',-cookie=>$co +okie); } else { &print_login; exit; } } else { &print_login; exit; } } print redirect('search.cgi'); exit; sub print_login { print header, start_html('Login'),h1('Login'), start_form, "Username: ",textfield('username'),br, "Password: ",password_field('password'),br, submit('Enter'), end_form, end_html; exit; }

In reply to [SOLVED] possible to repeat if statement? by jaffinito34

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.