how to confirm uname and pswd and allow access to next page.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gemstone
    New Member
    • Jul 2007
    • 9

    how to confirm uname and pswd and allow access to next page.

    hi guys,
    I have to accept the username and password entered on the web page(designed using html) store them in 2 variables (say, $var1 and $var2) and compare it with a text file(unpw.txt) which already has the list of username and its password (which will be split and stored as $uname and $pswd).
    i was told to use html in perl scripts. im having trouble with perl and also in including html in it.
    pls help.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    I haven't played with it as of yet, but when you are doing coding of web sites with Perl, you are delving into the realms of CGI. For an excellent intro tutorial on how CGI can be used, see this one located here at thescripts.com.

    That should show you how perl can interact with HTML, but you will definitely either need someone more fluent in CGI or to find a good tutorial on it.

    That said though, you may want to post the code that you have tried thus far as others will ask to see it as well. We are all very glad to assist, but you have to show what you have tried already.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      A far more gentle introduction to CGI programming with perl can be found here:



      Or a more indepth intro to CGI in general and CGI.pm in specifc can be found here (chapter 12):

      Comment

      • gemstone
        New Member
        • Jul 2007
        • 9

        #4
        hi jeff,
        this is what i've done so far. pls check if its correct.
        thanks.

        Code:
        #!/usr/bin/perl
        # Login CGI
         
        use CGI ':standard';
         
        use warnings;
        use strict;
         
        print "Content-type: text/html\n\n";
        print "<html>\n";
        print "<body>\n";
        print "<b><center><font size=7 color="#6699FF" face=arial>DELTAZIA</font></center></b>\n";
        print "<br><hr WIDTH="100%" COLOR="#6699FF" SIZE="6">
        <br><div style ="border:2px solid #CCCDDD; width:250px; height:220px; background-color:#99DDFF;">\n";
        print "<br><b><p align="center">LOGIN</p></b>\n";
         
        print "<p align="center">Username <input type=text size=20><br></p>
        <p align="center">Password <input type=password name=password size=20><br></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type=submit value=Login>&nbsp;&nbsp;
        <input type=reset value=Reset><br></p>
        </div>\n";
        print "</body>\n";
        print "</html>\n";
         
        $uname=param('Username');
        $pswd=param('Password');
        if ($uname eq '') {
        print"<h3><b>Kindly enter both username and passwork to proceed forward</b></h3>\n";
        sendfile("login1.html");
        }
         
        open (INF,"c:/minu/unpw.txt") or die "could not open the file: $!";
        while (<INF>) {
        ($un,$pw)=split(/:/);
        for ($_[0])
        if($uname eq $un) {
        if(($uname eq $un) && ($pswd eq $pw)) {
        sendfile("page22.html");
        } else {
        print "<h3><font color="red" face="arial">Invalid username or password</font></h3>\n";
        }
        } else {
        $_++;
        } else {
        print "<h3><font color="red" face="arial">Username or password does not exist</font></h3>\n";
        sendfile("login1.html");
        }
        }
         
        close(INF);
        Last edited by miller; Jul 20 '07, 07:47 AM. Reason: Code Tag and ReFormatting

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Its sort of correct. Ignoring all your syntax errors, you would write the routine that checks the name and password like so:

          Code:
          my $flag = 0;
          open (INF,"c:/minu/unpw.txt") or die "could not open the file: $!";
          while(<INF>){
             chomp;
             my ($un,$pw)= split(/:/);
             if ($uname eq $un && $pswd eq $pw){
                $flag = 1;
                last;
             }
          }
          if ($flag) {
             #login success
          }
          else {
             #login failed
          }

          Comment

          • gemstone
            New Member
            • Jul 2007
            • 9

            #6
            hi,
            thanks for checking it. i wanna know what does flag do in it. im new tp perl so i wanna know.

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              $flag is just used to determine if the login was a success or failure after the "while" loop finishes checking the user input against the file. $flag is initially given a value of 0 (zero) which in perl is considered a false value. During the "while" loop, if the name and password both match, $flag is changed to 1 (one) which is now a true value. "last" ends the "while" loop at the point. The if/else condition that follows checks the value of $flag. We do not need to check the actual value of $flag, we only need to check that it is a true (defined) value:

              Code:
              if ($flag)
              but you can write it like this if you prefer:

              Code:
              if ($flag == 1)
              using "flags" is common practice in programming. They are sometimes called binary flags because they can have only two values:

              1 (on/true)
              0 (off/false)

              that is not the only way you could something like this, but it's fairly common and It is most likely the way I would do it myself.

              Comment

              • gemstone
                New Member
                • Jul 2007
                • 9

                #8
                oh ok. so is this correct now?

                Code:
                #!/usr/bin/perl -T
                use strict;
                use warnings;
                use CGI;
                # LOGIN CGI;
                # use CGI ':standard';
                 
                print "Content-type: text/html\n\n";
                print "<html>\n";
                print "<form action=".../trial1july18.plx" method="post">
                print "<body>\n";
                print "<b><center><font size=7 color="#6699FF" face=arial>DELTAZIA</font></center></b>\n";
                print "<br><hr WIDTH="100%" COLOR="#6699FF" SIZE="6">
                <br><div style ="border:2px solid #CCCDDD; width:250px; height:220px; background-color:#99DDFF;">\n";
                print "<br><b><p align="center">LOGIN</p></b>\n";
                 
                print "<p align="center">Username <input type=text size=20><br></p>
                <p align="center">Password <input type=password name=password size=20><br></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type=submit value=Login>&nbsp;&nbsp;
                <input type=reset value=Reset><br></p></div>\n";
                print "</form>";
                print "</body>\n";
                print "</html>\n";
                $uname=param('Username');
                $pswd=param('Password');
                 
                if ($uname eq '') {
                print"<h3><b>Kindly enter both username and passwork to proceed forward</b></h3>\n";
                sendfile("login1.html");
                }
                 
                my $flag=0;
                open (INF,"c:/minu/unpw.txt") or die "could not open the file: $!";
                while (<INF>) {
                chomp;
                my ($un,$pw)=split(/:/);
                if($uname eq $un && $pswd eq $pw) {
                $flag=1;
                last;
                }
                }
                if ($flag) {
                sendfile("page22.html");
                } else {
                print "<h3><font color="red" face="arial">Invalid username or password</font></h3>\n";
                sendfile("login1.html");
                }
                 
                close(INF);
                Last edited by miller; Jul 20 '07, 07:50 AM. Reason: Code Tag and ReFormatting

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  It's getting there but the code overall will not work. You have not declared your variables with "my", and the overall logicl could be better.

                  I think we would all appreciate if you start using the code tags to post formatted code.

                  Comment

                  • miller
                    Recognized Expert Top Contributor
                    • Oct 2006
                    • 1086

                    #10
                    Please enclose your posted code in [CODE] tags (See How to Ask a Question).

                    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

                    Please use [CODE] tags in future.

                    - MODERATOR

                    Comment

                    • miller
                      Recognized Expert Top Contributor
                      • Oct 2006
                      • 1086

                      #11
                      Also, it would do you good to start ensuring that you code at least doesn't contain syntax errors before asking questions. Here is your code with the syntax errors fixed. The logic and overall design still needs to be fixed.

                      [CODE=perl]
                      #!/usr/bin/perl -T
                      use strict;
                      use warnings;
                      use CGI;
                      # LOGIN CGI;
                      # use CGI ':standard';

                      print <<"END_HTML";
                      Content-type: text/html

                      <html>
                      <form action=".../trial1july18.pl x" method="post">
                      <body>
                      <b><center><fon t size=7 color="#6699FF" face=arial>DELT AZIA</font></center></b>
                      <br><hr WIDTH="100%" COLOR="#6699FF" SIZE="6">
                      <br><div style ="border:2px solid #CCCDDD; width:250px; height:220px; background-color:#99DDFF;" >
                      <br><b><p align="center"> LOGIN</p></b>
                      <p align="center"> Username <input type=text size=20><br></p>
                      <p align="center"> Password <input type=password name=password size=20><br></p><p>&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;
                      <input type=submit value=Login>&nb sp;&nbsp;
                      <input type=reset value=Reset><br ></p></div>
                      </form>
                      </body>
                      </html>
                      END_HTML

                      my $uname = param('Username ');
                      my $pswd = param('Password ');

                      if ($uname eq '') {
                      print qq{<h3><b>Kindl y enter both username and passwork to proceed forward</b></h3>\n};
                      sendfile("login 1.html");
                      }

                      my $flag = 0;
                      open(INF, "c:/minu/unpw.txt") or die "could not open the file: $!";
                      while (<INF>) {
                      chomp;
                      my ($un, $pw) = split ':';
                      if ($uname eq $un && $pswd eq $pw) {
                      $flag=1;
                      last;
                      }
                      }
                      close(INF);

                      if ($flag) {
                      sendfile("page2 2.html");
                      } else {
                      print qq{<h3><font color="red" face="arial">In valid username or password</font></h3>\n};
                      sendfile("login 1.html");
                      }

                      close(INF);
                      [/CODE]

                      - Miller

                      Comment

                      • gemstone
                        New Member
                        • Jul 2007
                        • 9

                        #12
                        you have used close (INF); twiceis that possible?

                        Originally posted by miller
                        Also, it would do you good to start ensuring that you code at least doesn't contain syntax errors before asking questions. Here is your code with the syntax errors fixed. The logic and overall design still needs to be fixed.

                        [CODE=perl]
                        #!/usr/bin/perl -T
                        use strict;
                        use warnings;
                        use CGI;
                        # LOGIN CGI;
                        # use CGI ':standard';

                        print <<"END_HTML";
                        Content-type: text/html

                        <html>
                        <form action=".../trial1july18.pl x" method="post">
                        <body>
                        <b><center><fon t size=7 color="#6699FF" face=arial>DELT AZIA</font></center></b>
                        <br><hr WIDTH="100%" COLOR="#6699FF" SIZE="6">
                        <br><div style ="border:2px solid #CCCDDD; width:250px; height:220px; background-color:#99DDFF;" >
                        <br><b><p align="center"> LOGIN</p></b>
                        <p align="center"> Username <input type=text size=20><br></p>
                        <p align="center"> Password <input type=password name=password size=20><br></p><p>&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;
                        <input type=submit value=Login>&nb sp;&nbsp;
                        <input type=reset value=Reset><br ></p></div>
                        </form>
                        </body>
                        </html>
                        END_HTML

                        my $uname = param('Username ');
                        my $pswd = param('Password ');

                        if ($uname eq '') {
                        print qq{<h3><b>Kindl y enter both username and passwork to proceed forward</b></h3>\n};
                        sendfile("login 1.html");
                        }

                        my $flag = 0;
                        open(INF, "c:/minu/unpw.txt") or die "could not open the file: $!";
                        while (<INF>) {
                        chomp;
                        my ($un, $pw) = split ':';
                        if ($uname eq $un && $pswd eq $pw) {
                        $flag=1;
                        last;
                        }
                        }
                        close(INF);

                        if ($flag) {
                        sendfile("page2 2.html");
                        } else {
                        print qq{<h3><font color="red" face="arial">In valid username or password</font></h3>\n};
                        sendfile("login 1.html");
                        }

                        close(INF);
                        [/CODE]

                        - Miller

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          hehehe.... are you speechless gemstone? ;)

                          Comment

                          • gemstone
                            New Member
                            • Jul 2007
                            • 9

                            #14
                            not speechless exactly but im trying to polish what i've done so far. got lots more to do.

                            Originally posted by KevinADC
                            hehehe.... are you speechless gemstone? ;)

                            Comment

                            Working...