2010-12-17 162 views
2

我有嘗試檢查密碼的複雜下面的PHP代碼然而,當我運行它,我似乎並沒有得到有效的結果(見下文)PHP密碼複雜性檢查代碼

while (odbc_fetch_row($rs)) { 

    $r1='/[A-Z]/'; //Uppercase 
    $r2='/[a-z]/'; //lowercase 
    $r3='/[[email protected]#$%^&*()-_=+{};:,<.>]/'; // whatever you mean by 'special char' 
    $r4='/[0-9]/'; //numbers 


if (!preg_match_all($r4, odbc_result($rs,"U_password"))) { 
    echo "Password doesn't contain numbers: " . odbc_result($rs,"U_Mailbox") . "@" . odbc_result($rs,"U_Domain") . " " . odbc_result($rs,"U_password") . "<br>\n"; 
} 

if (!preg_match_all($r2, odbc_result($rs,"U_password"))) { 
    echo "Password doesn't contain lowercase letters: " . odbc_result($rs,"U_Mailbox") . "@" . odbc_result($rs,"U_Domain") . " " . odbc_result($rs,"U_password") . "<br>\n"; 
} 

if (!preg_match_all($r1, odbc_result($rs,"U_password"))) { 
    echo "Password doesn't contain uppercase letters: " . odbc_result($rs,"U_Mailbox") . "@" . odbc_result($rs,"U_Domain") . " " . odbc_result($rs,"U_password") . "<br>\n"; 
} 

}

輸出:

密碼不包含小寫字母:[email protected] lowercasepw
密碼不包含足夠的數字:[email protected] 8B_dip + 3
密碼不包含大寫字母:[email protected] Barrywidg @ t1o

(順便說一句,上面的密碼是不真實的。我只是將小寫字母替換爲其他小寫字母,其他大寫字母替換爲大寫字母,其他數字替換爲其他數字。)

所以我很困惑,爲什麼我的代碼不工作。其他人可以幫忙嗎?

感謝 布拉德

回答

4
  1. 變化preg_match_allpreg_match
  2. 在您的特殊字符正則表達式中,您有幾個不會轉義的正則表達式字符。這是esacaped:/[\[email protected]#\$%\^&\*\(\)-_=+\{\};:,<\.>]/

沒有保證,這就是雖然問題的100%......

+0

謝謝 - 這似乎是它。 – Brad 2010-12-17 23:44:57

1

preg_match_all接受三個參數,而不是兩個:第三個是一個數組,將填充參考與火柴。使用preg_match

此外,確保這些特殊字符如果屬於PCRE語法的一部分則會被轉義(例如:\.而不是.)。

我建議你增加error_reporting和/或激活display_errors和/或錯誤記錄。如果有的話,這是因爲吐出E_WARNING錯誤而被捕獲的東西。

+0

謝謝,我正在檢查phperror.log,但也許我沒有足夠高的測井曲線。 – Brad 2010-12-17 23:45:37

2

後轉換你的榜樣,以獨立代碼(請張貼在未來)

我得到這些一堆:

PHP Warning: preg_match_all() expects at least 3 parameters, 2 given in /Users/cabbey/foo.php on line 9

你或許應該打開了錯誤報告。

我想既然你使用_all,你想算來,在這種情況下,你想改變你的正則表達式的不可貪了。

$r1='/[A-Z]{1}/'; //Uppercase 
$r2='/[a-z]{1}/'; //lowercase 
$r3='/[[email protected]#$%^&*()_=+{};:,<.>-]{1}/'; // whatever you mean by 'special char' 
$r4='/[0-9]{1}/'; //numbers 

$found = array(); 

foreach (array('lowercasepw', '8B_dip+3', '[email protected]') as $pass) { 

    if (!preg_match_all($r4, $pass, $found)) { 
     echo "Password doesn't contain numbers: $pass\n"; 
    } else { 
     echo "found ".count($found[0])." numbers\n"; 
    } 

    if (!preg_match_all($r2,$pass, $found)) { 
     echo "Password doesn't contain lowercase letters: $pass\n"; 
    } else { 
     echo "found ".count($found[0])." lowercase\n"; 
    } 

    if (!preg_match_all($r1, $pass, $found)) { 
     echo "Password doesn't contain uppercase letters: $pass\n"; 
    } else { 
     echo "found ".count($found[0])." uppercase\n"; 
    } 

}