我正在制作一個注冊頁面,一切正常并發送到資料庫,但您可以輸入弱密碼。我想確保密碼長度的最小長度為 8。我添加了這些代碼行,但是當我測驗它時,它跳過了這段代碼,你可以輸入任何你想要的密碼。有誰知道為什么這條線被跳過了,這個問題的解決方案是什么?
function pwdTooShort($pwd) {
$result;
if (strlen($pwd) > 7) {
$result = true;
}
else{
$result = false;
}
return $result;
}
if (isset($_POST["submit"])) {
$pwd = $_POST["pwd"];
require_once 'functions.inc.php';
if(pwdTooShort($pwd) !== false) {
header("location: ../sign-in.php?error=passwordtooshort");
exit();
}
}
if (isset($_GET["error"])){
if($_GET["error"] == "passwordtooshort"){
echo "<p> password is too short </p>";
}
}
<form action="include/signup.inc.php" method = "post">
<input type="password" name = "pwd" />
</form>
uj5u.com熱心網友回復:
你有一些邏輯問題。
如果密碼超過 7 個字符(向后),您pwdTooShort()現在將回傳true。您可以將該功能更改為:
function pwdTooShort($pwd)
{
// Return true if it is 7 characters or shorter
return mb_strlen($pwd) <= 7;
}
我也改變strlen()到mb_strlen()以考慮多位元組字符,如@vee的意見建議。
改進建議
-if陳述句在技術上是正確的,但“過于復雜”。
你可以改變
if (pwdTooShort($pwd) !== false)
要么
if (pwdTooShort($pwd) === true)
要不就
if (pwdTooShort($pwd))
為了更容易閱讀
uj5u.com熱心網友回復:
從你的函式名到檢查密碼太短,我認為true如果太短,false如果不是,它應該回傳。
這是代碼。它只是翻轉true和false。
/**
* Check if password is too short (less than 8 characters).
*
* @param string $pwd The raw password without hash.
* @return bool Return `true` if it is too short, return `false` if it is not.
*/
function pwdTooShort($pwd) {
$result;
if (mb_strlen($pwd) > 7) {
$result = false;
}
else{
$result = true;
}
return $result;
}
上面的代碼,我從strlen()改為mb_strlen()讓它支持多位元組字符(unicode文本)并精確計算字符。
我建議您不要將字符限制為非 unicode。這是OWASP推薦的。
允許使用所有字符,包括 unicode 和空格。不應該有限制允許的字符型別的密碼組合規則。
他們所說的空格意味著字符之間。你還可以trim($password)。
其余的代碼將適用于此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/391625.html
上一篇:為什么我的Python腳本在使用函式并使用input(print())后列印“None”?
下一篇:基于文本的游戲。主功能
