2012-11-15 17 views
-1

如果他們登錄成功,我需要打印他們的工資支票數據,我正在進行一項工作,我必須讓用戶登錄(根據文件檢查他們的用戶名/密碼)它存儲在另一個名爲IN-accounting.data的文件中。我設法讓用戶登錄部分工作,但我無法打印「paycheck」部分,因爲它現在它將在會計文件中打印每個人,我可以使用一些幫助瞭解如何僅打印數據用戶登錄了任何建議? 所有SSN,電話號碼,在這些文件和代碼的地址和人是假的,他們提供給做作業perl用戶登錄然後打印他們的工資支票數據


12年11月6日更新,以反映建議的更改,現在運行得數據順便我本來也想


#!/usr/bin/perl 
use warnings; 
use strict; 
use Digest::MD5 'md5_hex'; 

open (PSWD, '<', 'password.passwd'); 
#getting username and password 
#converting username to lowercase if username is entered in CAPS 
print "Please enter your username: "; 
chomp(my $userN = <STDIN>); 
my $username = lc($userN); 
############################################ 
print "Please enter your password: "; 
chomp(my $password = <STDIN>); 
my $passwd=md5_hex($password); 
############################################### 
my $matchCount = 0;#used later to make sure username and password match file 
#reading password.passwd and assigning values 
while (my $lines = <PSWD>){ 
my($user,$pswd,$userID,$groupID,$info,$home,$shell) = split ':', $lines; 

#checking username entered vs that in the passwd file 
if ($username eq $user){ 
print "Checking username... MATCH\n"; 
$username=$info; 
#keeps track if username matches or not 
$matchCount+=1; 

#checking password entered vs that in the passwd file 
if ($passwd eq $pswd){ 
    print "Checking password... MATCH\n"; 
     my ($first,$last)=split(" ", $info); 
     accounting($first,$last); 
} 
else{ 
     print "Password does not match!\n"; 
    } 
    last; 


} 
} 

# if matchcount did not change, username did not match killing the program 
if ($matchCount == 0){ 
    die ("\"$username\" does not match any users in our database!\n"); 
} 





sub accounting{ 
    my $first_name=shift; 
    my $last_name=shift; 
    open(my $fh, '<', 'IN-accounting.data') or die "cannot open accounting file $!"; 
    while (my $lines = <$fh>){ 
    chomp $lines; 
     my @fields = split(/\|/, $lines); 

     push @data2, \@fields; 



    my($Lname,$Fname,$ssn,$address)=($fields[0],$fields[1],$fields[2],$fields[3]); 
     my($city,$state,$zip,$payDate)=($fields[4],$fields[5],$fields[6],$fields[7]); 
     my($hours,$rate,$taxes,$deductions,$notes)= ($fields[8],$fields[9],$fields[10],$fields[11],$fields[12]); 

     next if $Lname ne $last_name and $Fname ne $first_name; 

    my ($Gpay)= eval($hours)*eval($rate);#gross pay 
    my ($Tpay)=$Gpay-($taxes+$deductions);#total pay 
    my $Essn=substr($ssn,+-4);#edited ssn 
    print "$Fname $Lname\n"; 
    print "$address\n"; 
    print "$city $state $zip\n"; 
    print "SSN: xxx-xx-$Essn\n"; 
    print"\n"; 
    print "Pay Date: $payDate"; 
    print"\n"; 
    print"You had $hours hours at \$$rate/hour\n"; 
    print"Gross Pay: $Gpay\n"; 
    print"Taxes:\$-$taxes\n"; 
    print"Deductions:\$-$deductions\n"; 
    print"Total Pay: $Tpay\n"; 
    print"\n"; 
    print"Notes:$notes\n\n"; 



} 
print"press enter to quit: "; 
     my $quit=<>;  
     if ($quit){ exit;} 
} 

password.passwd

amon9640:4cb9c8a8048fd02294477fcb1a41191a:500:25:亞歷山大週一:/首頁/工資:/ bin中/ PA yroll iart1373:4cb9c8a8048fd02294477fcb1a41191a:501:25:伊尼戈Arterbury:/首頁/工資:/斌/工資 wher0210:4cb9c8a8048fd02294477fcb1a41191a:502:25:沃德爾赫爾曼:/首頁/工資:/斌/工資

會計文件

星期一|亞歷山大| 815-19-9640 | 4662 Dewy細分| Owltown |俄勒岡| 97434-8480 | 1/18/1998 | 19 | 21.68 | 60.28 | 2.24 |工資覈算審計帳戶,並且報告將到期不久。

Arterbury | Inigo | 037-30-1373 | 987 Rocky Island Byway |聖誕節城市|新墨西哥| 88023-3889 | 4/1/1993 | 9 | 7.02 | 17.75 | 12.71 |審計完成。發現缺陷。

Herman | Wardell | 114-29-0210 | 5555 Cinder Forest Wynd | White Eyes Town |華盛頓| 98707-5628 | 10/0/2003 | 37 | 3.07 | 41.90 | 20.89 |審覈完成。發現缺陷。

+0

誰將使用此腳本?如果它是與會計文件相同系統上的用戶,則它們既可以訪問腳本也可以訪問文件,否則腳本將無法運行。他們只能讀取文件或從腳本中刪除密碼檢查 – JRideout

+0

MD5是用於密碼的可怕哈希值:[加密哈希函數不是密碼哈希函數](http://throwingfire.com/storing-passwords-securely/# notpasswordhashes) – JRideout

+0

從用戶的應用程序邏輯中分離出數據的加載和解析可能是一個好主意,它登錄並格式化輸出記錄。將數據放入某種關係數據庫也可能是一種改進 - 它們是爲這種類型的事情而構建的。 – JRideout

回答

0

我會跳過安全問題。讓我們看幾件事:

你的accounting子程序假設要做什麼?打印用戶的薪水?您沒有將任何數據傳遞給子例程。子程序如何知道打印哪個用戶的薪水?

爲什麼不這樣做?現在

accounting($Fname, $Lname); 

sub accounting { 
    my $first_name = shift; 
    my $last_name = shift; 

    if (not $first_name or not $last_name) { 
     die "You need a first name and a last name...) 
    } 

,您可以通過會計文件一行一行地循環,尋找線,其中$first_name$last_name匹配。

while (my $lines = <$fh>){ 
    chomp $lines; 

    my ($Lname, $Fname, $ssn, $address, 
     $city, $state, $zip, $payDatek, 
     $hours, $rate, $taxes,$deductions, $notes) = split /\|/, $lines; 

     #Skip over non matching lines 
     next if $Lname ne $last_name and $Fname ne $first_name; 

你已經在你的代碼有一些虛假的錯誤。幾個簡單的人,我注意到:

  • push @data2, \@fields;你推一個數組的引用到一個數組。
  • my ($Lname, $Fname, ... $notes) = %fields;字段是一個數組,而不是散列。

我很驚訝,你的這些問題已編譯的程序。

而且,請在代碼中使用空格。它使閱讀和遵循更容易。

+0

我覺得自己像一個白癡我怎麼可能看不到我沒有傳遞任何東西給我的子例程?感謝您指出了這一點,生病開始努力糾正一切,並讓您知道一切如何 – user1819703

0

哇,還有在這裏你的方法,一些嚴重的安全問題。我會盡量在評論中指出幾點。

要專注於您的具體問題,您需要某種方式將用戶鏈接到會計數據,然後按用戶進行過濾。理想情況下,您會將原始數據攝入關係約束數據庫中,以使其更容易。變量名返回的正則表達式的字段映射後

next unless "$Fname $Lname" eq $username; 

廣場這樣的:但目前的做法,你可以做這樣的事情。

相關問題