簡體   English   中英

php文檔,用於檢查預定義密碼數組的錯誤

[英]php document that checks errors of an array of predefined passwords

在我的程序中,我必須檢查密碼失敗的原因。 長度(如果存在空格),如果有數字,小寫字母,大寫字母和非字母數字符號。 我正在進行所有測試,但密碼長度正確錯誤。 我一直在嘗試環顧四周,而不復制和粘貼某人的作品,並嘗試了具有相同結果的不同方法。 我是php新手,所以可能是語法濫用或其他錯誤,但這是我的代碼。

$password=array("fajsldkL^","falsd L8&","JLKJOIH9*","fjdsllsk9*","fjasldkfK8","fjl*9K","djflsK909*dkK","fdkjslK9*","fjslaKLK98*","fjasdlkKJ87*");
$goodPassword="fjasdlkKJ87*";
$count=0; // helps manually cycle through the array because i dont know how else to with foreach

foreach($password as $goodPassword)
{
  $length=array_map('strlen', $password); //supposed to get length of the array element
  $invalidLength=true;
  $whitespace=false;
  $numerical=false;
  $lower=false;
  $upper=false;
  $special=false;

  echo "Your password: $password[$count] </br>";
  $count++;

  if ($length>=8 && $length<=16) //problem area
      $invalidLength=false;
  if (preg_match("/\s/", $goodPassword) ==1)
      $whitespace=true;
  if (preg_match("/[0-9]/", $goodPassword) ==0)
      $numerical=true;
  if (preg_match("/[a-z]/", $goodPassword) ==0)
      $lower=true;
  if (preg_match("/[A-Z]/", $goodPassword) ==0)
      $upper=true;
  if (preg_match("/\W/", $goodPassword) ==0)
      $special=true;

  if ($invalidLength==true)
      echo "Password is not between 8 and 16 characters. </br>";
  if ($whitespace==true)
      echo "Password should not have any spaces. </br>";
  if ($numerical==true)
      echo "Password needs at least 1 number. </br>";
  if ($lower==true)
      echo "Password needs at least 1 lowercase letter. </br>";
  if ($upper==true)
      echo "Password needs at least 1 uppercase letter. </br>";
  if ($special==true)
      echo "Password needs at least 1 none alphanumerical symobl. </br>";

  if ($invalidLength==false && $whitespace==false && $numerical==false && $lower==false && $upper==false && $special==false)
      echo "Password is good. </br>";
}

fjl * 9K應該不會通過長度測試,但是如果我翻轉它們,它們要么全部失敗,要么此元素及其后面的元素沒有失敗,但它不會失敗。 非常感謝您的幫助。 如果這只是一個簡單的邏輯錯誤,我將感到非常愚蠢。

編寫如下代碼:

$password=array("fajsldkL^","falsd L8&","JLKJOIH9*","fjdsllsk9*","fjasldkfK8","fjl*9K","djflsK909*dkK","fdkjslK9*","fjslaKLK98*","fjasdlkKJ87*");
$goodPassword="fjasdlkKJ87*";
// $count=0; // You don't need to use count

foreach($password as $goodPassword)
{
  // Here is wrong. strlen is used to count lenth of string. Here $password is array
  //$length=array_map('strlen', $password); // Write $goodPassword Here
  // Write your variable as below:-
  $length = strlen($goodPassword);
  $invalidLength=true;
  $whitespace=false;
  $numerical=false;
  $lower=false;
  $upper=false;
  $special=false;
  // Write $goodPassword here
  echo "Your password: $goodPassword </br>";
  // $count++; // No need here

  if ($length>=8 && $length<=16) //problem area
      $invalidLength=false;
  if (preg_match("/\s/", $goodPassword) ==1)
      $whitespace=true;
  if (preg_match("/[0-9]/", $goodPassword) ==0)
      $numerical=true;
  if (preg_match("/[a-z]/", $goodPassword) ==0)
      $lower=true;
  if (preg_match("/[A-Z]/", $goodPassword) ==0)
      $upper=true;
  if (preg_match("/\W/", $goodPassword) ==0)
      $special=true;

  if ($invalidLength==true)
      echo "Password is not between 8 and 16 characters. </br>";
  if ($whitespace==true)
      echo "Password should not have any spaces. </br>";
  if ($numerical==true)
      echo "Password needs at least 1 number. </br>";
  if ($lower==true)
      echo "Password needs at least 1 lowercase letter. </br>";
  if ($upper==true)
      echo "Password needs at least 1 uppercase letter. </br>";
  if ($special==true)
      echo "Password needs at least 1 none alphanumerical symobl. </br>";

  if ($invalidLength==false && $whitespace==false && $numerical==false && $lower==false && $upper==false && $special==false)
      // Write $goodPassword in below line:-
      echo "Password $goodPassword is good. </br>";
}

希望它能對您有所幫助:)

$passwords = array('fajsldkL^', 'falsd L8&', 'JLKJOIH9*', 'fjdsllsk9*', 'fjasldkfK8', 'fjl*9K', 'djflsK909*dkK', 'fdkjslK9*', 'fjslaKLK98*', 'fjasdlkKJ87*'); // lets name this plural since it contains lots of passwords. also use single quote for literal strings

// $goodPassword = 'fjasdlkKJ87*';  // lets remove this since we will use the loop
// $count        = 0; // lets remove this

foreach ($passwords as $password) { // notice the plural and singular, its how you use foreach, its readable that way
    $length        = strlen($password); // this is the correct way
    $invalidLength = true;
    $whitespace    = false;
    $numerical     = false;
    $lower         = false;
    $upper         = false;
    $special       = false;

    echo "Your password: $password </br>"; // use the variable $password also use double quote for parsed string since its the alias of the array's item
    // $count++; //let remove this, we dont need this

    if ($length >= 8 && $length <= 16) //problem area
    {
        $invalidLength = false;
    }

    if (preg_match("/\s/", $password) == 1) {
        $whitespace = true;
    }

    if (preg_match("/[0-9]/", $password) == 0) {
        $numerical = true;
    }

    if (preg_match("/[a-z]/", $password) == 0) {
        $lower = true;
    }

    if (preg_match("/[A-Z]/", $password) == 0) {
        $upper = true;
    }

    if (preg_match("/\W/", $password) == 0) {
        $special = true;
    }

    if ($invalidLength == true) {
        echo 'Password is not between 8 and 16 characters. </br>';
    }

    if ($whitespace == true) {
        echo 'Password should not have any spaces. </br>';
    }

    if ($numerical == true) {
        echo 'Password needs at least 1 number. </br>';
    }

    if ($lower == true) {
        echo 'Password needs at least 1 lowercase letter. </br>';
    }

    if ($upper == true) {
        echo 'Password needs at least 1 uppercase letter. </br>';
    }

    if ($special == true) {
        echo 'Password needs at least 1 none alphanumerical symobl. </br>';
    }

    if ($invalidLength == false && $whitespace == false && $numerical == false && $lower == false && $upper == false && $special == false) {
        echo 'Password is good. </br>';
    }

}

您也可以這樣寫:

$passwords = array('fajsldkL^', 'falsd L8&', 'JLKJOIH9*', 'fjdsllsk9*', 'fjasldkfK8', 'fjl*9K', 'djflsK909*dkK', 'fdkjslK9*', 'fjslaKLK98*', 'fjasdlkKJ87*');

foreach ($passwords as $password) {
    echo "</br>Your password: $password</br>";

    $invalid_len     = false;
    $with_space      = false;
    $without_num     = false;
    $without_lower   = false;
    $without_upper   = false;
    $without_special = false;

    $length = strlen($password);

    if ($length < 8 || $length > 16) {
        $invalid_len = true;
        echo 'Password is not between 8 and 16 characters.</br>';
    }

    if (preg_match("/\s/", $password) == 1) {
        $with_space = true;
        echo 'Password should not have any spaces.</br>';
    }

    if (preg_match("/[0-9]/", $password) == 0) {
        $without_num = true;
        echo 'Password needs at least 1 number.</br>';
    }

    if (preg_match("/[a-z]/", $password) == 0) {
        $without_num = true;
        echo 'Password needs at least 1 lowercase letter.</br>';
    }

    if (preg_match("/[A-Z]/", $password) == 0) {
        $without_upper = true;
        echo 'Password needs at least 1 uppercase letter.</br>';
    }

    if (preg_match("/\W/", $password) == 0) {
        $without_special = true;
        echo 'Password needs at least 1 none alphabumerical symbol.</br>';
    }

    if ($invalid_len == false && $with_space == false && $without_num == false && $without_num == false && $without_upper == false && $without_special == false) {
        echo 'Password is good.</br>';
    }
}

在for循環中,您使用了$ length = array_map('strlen',$ password);。 我們可以直接用作$ lenght = strlen($ password [$ count]); 所以代碼將是

“; $ count ++; if($ length> = 8 && $ length”; if($ whitespace == true)echo“密碼不應有任何空格。”; if($ numerical == true)echo“密碼至少需要1個數字。;; if($ lower == true)echo“密碼需要至少1個小寫字母。”; if($ upper == true)echo“ Password需要至少1個大寫字母。”; if($ special = = true)echo“密碼至少需要1個字母數字符號。”;如果($ invalidLength == false && $ whitespace == false && $ numerical == false && $ lower == false && $ upper == false && $ special == false)echo“密碼好。”;}?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM