用于检查预定义密码数组错误的php文档

时间:2016-02-15 06:52:14

标签: php

在我的程序中,我必须检查密码失败的原因。长度,如果存在空格,如果有数字,小写字母,大写字母和非字母数字符号。我正在获得所有测试,除了密码长度正确错误。我一直试图环顾四周,没有复制和粘贴某人的工作,并尝试了不同的方法,结果相同。我是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应该在长度测试中失败,但它没有,如果我翻转它们或者全部失败,或者这个元素和它之后的那些没有失败。非常感谢您的帮助。如果它只是一个简单的逻辑错误,我会感到很愚蠢。

3 个答案:

答案 0 :(得分:0)

编写如下代码: -

$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>";
}

希望它会对你有所帮助:)。

答案 1 :(得分:0)

$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>';
    }
}

答案 2 :(得分:0)

在for循环中你使用了$ length = array_map('strlen',$ password);我们可以直接使用$ lenght = strlen($ password [$ count]);所以代码将是

“;             $计数++;             if($ length&gt; = 8&amp;&amp; $ length“;             if($ whitespace == true)                 echo“密码不应该有任何空格。”;             if($ numerical == true)                 echo“密码至少需要1个号码。”;             if($ lower == true)                 echo“密码至少需要1个小写字母。”;             if($ upper == true)                 echo“密码至少需要1个大写字母。”;             if($ special == true)                 echo“密码至少需要1个字母数字symobl。”;             if($ invalidLength == false&amp;&amp; $ whitespace == false&amp;&amp; $ numerical == false&amp;&amp; $ lower == false&amp;&amp; $ upper == false&amp;&amp; $ special = = FALSE)                 echo“密码很好。”;         } ?&GT;