⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fig16_10.pl

📁 PERL语言资料 可以用于PERL程序设计
💻 PL
字号:
#!/usr/bin/perl
# Fig 16.10: fig16_10.pl
# Script to login to a Web page.

use strict;
use warnings;
use CGI qw( :standard );
use Digest::MD5;
use Digest::MD5 qw( md5_hex );

# parameters are read in from the previous Web page
my $password = param( "password" );
my $user = param( "user" );
my $new = param( "new" );

if ( param( "new" ) ) {
   my @search1;

   # Write nothing to the cookie ---
   # Users just logging in will not have a cart
   writeCookie();

   # Encryption of password so that it is protected
   my $encrypt;
   my $digestObject = Digest::MD5->new();
   $digestObject->add( "$password" );
   $encrypt = $digestObject->digest();

   print( header() );
   print( start_html() );

   open( LOG, "<log.txt" ) or die( "Cannot open: $!" );

   while ( <LOG> ) {
      @search1 = split( "\t" );

      # If this username is found in the log,
      # it has already been taken
      if ( $search1[ 0 ] eq $user ) {
         print( "This name has already been taken.<br>" );
         print( "<a href = \"fig16_10.pl\">Try again</a>." );
         print( end_html() );
         exit;
      }
   }
   close( LOG ) or die( "Cannot close: $!" );

   # Add new user to the file
   open( LOG, ">>log.txt" ) or die( "Cannot open: $!" );
   print( LOG "$user\t$encrypt\n" );
   close( LOG ) or die( "Cannot close: $!" );

   print( i( "Your information has been processed." ) );
   print( br() );
   print( "<a href = \"fig16_11.pl\">Start Shopping!</a>" );
   print( end_html() );
}
elsif ( param( "password" ) ) {
   my $found = 0;
   my @search2;

   writeCookie();
   print( header(), start_html() );

   my $digestObject = Digest::MD5->new();
   $digestObject->add( "$password" );
   my $encrypt = $digestObject->digest();

   # search the log for this person
   open( LOG, "<log.txt" ) or die( "Cannot open: $!" );

   while ( <LOG> ) {
      @search2 = split( "\t" );
      chomp( $search2[ 1 ] );

      if ( $search2[ 0 ] eq $user ) {

         # The password entered is correct
         if ( $search2[ 1 ] eq $encrypt ) {
            print( "Thank you for returning, $user!" );
            print( br() );
            print( "<a href = \"fig16_11.pl\">Start Shopping!" );
            print( "</a>" );
            $found = 1;
            last;  
         }
         else {
            print( i( "You have entered an incorrect " ) );
            print( i( "password. Please try again." ) );
            print( br() );
            print( "<a href = \"fig16_10.pl\">Back to login" );
            print( "</a>" );
            $found = 1;
            last;
         }
      }  
   }
   close( LOG );

   # This person is not found in the log
   # They are new or have entered an incorrect username
   if ( $found == 0 ) {
      print( "You are not a registered user.<br>" );
      print( "<a href = \"fig16_10.pl\">Register</a>" );
   }
   print( end_html() );
}
else {
   print( header() );
   print( start_html( -title => 'Please login' ) );

   print <<"   FORM";

   <p>Please login.</p>

   <form method = "post" action = "fig16_10.pl"><p>

   User Name: <input type = "text" name = "user"><br/>
   Password: <input type = "password" name = "password"><br/>
   New? <input type = "checkbox" name = "new" value = "1"></p>

   <input type = "submit" value = "login">
   </form>

   FORM

   print( end_html() );
}

# Function writeCookie creates a cookie containing
# the array that was passed in during the function call
sub writeCookie
{
   my $expires = "Monday, 11-JUN-01 16:00:00 GMT";
   print( "Set-Cookie: " );
   print( "CART=", join( "\t", @_ ), "; expires=$expires\n" );
   return;
}

###########################################################################
#  (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall.     #
#  All Rights Reserved.                                                   #
#                                                                         #
#  DISCLAIMER: The authors and publisher of this book have used their     #
#  best efforts in preparing the book. These efforts include the          #
#  development, research, and testing of the theories and programs        #
#  to determine their effectiveness. The authors and publisher make       #
#  no warranty of any kind, expressed or implied, with regard to these    #
#  programs or to the documentation contained in these books. The authors #
#  and publisher shall not be liable in any event for incidental or       #
#  consequential damages in connection with, or arising out of, the       #
#  furnishing, performance, or use of these programs.                     #
###########################################################################

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -