【问题标题】:PHP - Checking values of an arrayPHP - 检查数组的值
【发布时间】:2013-02-19 11:56:27
【问题描述】:

我正在尝试做一些具体的事情。我需要比较数组的每个元素中的用户名和密码,以找到与现有用户的匹配项。

有两个数组。一个包含所有用户信息。另一个包含登录尝试。练习是在登录尝试匹配时打印出用户信息。所以我需要将 $loginInfo 与 $userData 进行比较,以查看是否有任何登录尝试与存储的用户名和密码匹配。

在本练习中,我还需要使用 substr()、md5() 和 strtolower()。用户名不区分大小写,而密码区分大小写。我不确定我应该怎么做,但我可以在用户名上使用 strtolower(),但我也在寻找 md5 哈希中的最后 8 个字符。也不知道我是怎么做到的。我将密码哈希的最后 8 个字符与登录尝试哈希进行比较。

我觉得这会让所有试图提供帮助的人感到困惑。这显然让我感到困惑。

我附上了我的代码,希望能帮助你更清楚地理解这一点。

提前致谢!

<?php

$userData = array();

$userData[] = array(
              'Name' => 'Joe Banks',
              'Acct' => '12345',
              'Email' => 'joe@home.com',
              'UserName' => 'Joe',
              'Password' => '8e549b63',
              'Active' => false);
              'Password' => 'Password1'
$userData[] = array(
              'Name' => 'Polly Cartwrite',
              'Acct' => '34567',
              'Email' => 'polly@yahoo.com',
              'UserName' => 'PCart',
              'Password' => '91f84e7b',
              'Active' => true);
              'Password' => '12345'
$userData[] = array(
              'Name' => 'Jake Jarvis',
              'Acct' => '81812',
              'Email' => 'jjar@gmail.com',
              'UserName' => 'jakej',
              'Password' => 'd5cc072e',
              'Active' => true);
              'Password' => 'LetMeIn'
$userData[] = array(
              'Name' => 'Kelly Williams',
              'Acct' => '76253',
              'Email' => 'kw1234@yahoo.com',
              'UserName' => 'kellyw',
              'Password' => '2d635fc7',
              'Active' => false);
              'Password' => 'Kelly'
 $userData[] = array(
              'Name' => 'Cindy Ella',
              'Acct' => '62341',
              'Email' => 'washgirl@momsplace.com',
              'UserName' => 'Cinders',
              'Password' => '87c0e367',
              'Active' => true);
              'Password' => '9Kut!5pw'

// The loginInfo array contains a series of login attempts. Each attempt
// is composed of a username and password
$loginInfo = array();
$loginInfo[] = array('joe','hello');
$loginInfo[] = array('PCART','12345');
$loginInfo[] = array('jakej','letmein');
$loginInfo[] = array('KellyW','Kelly');
$loginInfo[] = array('Cinder','9Kut!5pw');

// function printUser()
// inputs:
//   $user - an array containing the user's data. The expectation is that
//           this array will contain the user's name, password, username,
//           active status, account number and email address
// outputs:
//   n/a
// This function will print out all of the information for a particular
// user in tabular format (with the exception of the password which will
// be suppressed).
function printUser($user) {

// Each user will be printed in its own row in the table
echo "<div class='tablerow'>\n";

foreach ($user as $index => $item) {
  // suppress printing the password
  if ($index == "Password")
    continue;

  // pretty print the user's status
  if ($index == "Active")  {
    if ($item) {
      $item = "active";
    } else {
      $item = "inactive";
    }
  }

  // print the data in a tabledata box
  echo "<div class='tabledata'>$item</div>\n";
}

// end the row
echo "</div>\n";
}


function checkLogin($loginInfo){
global $userData;

foreach($userData as $attempt) {
    if($loginInfo[$attempt][0] == $userData['UserName']){
      if($loginInfo[$attempt][1] == $userData['Password']){
        printUser($userData);
      }
    }
 }
}
checkLogin($loginInfo);
?>

【问题讨论】:

标签: php arrays string compare


【解决方案1】:

您需要两个循环来比较 2 个数组的值。试试这个:

for($i=0;$i<count($loginInfo);$i++){
    foreach($userData as $attempt) {
        if($loginInfo[$i][0] == $attempt['UserName']){
            if($loginInfo[$i][1] == $attempt['Password']){
                printUser($attempt);
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    我注意到您的用户数据数组中有 2 个“密码”字段,第一个密码字段似乎是第二个密码字段的 md5 哈希的最后 8 个字符,如果以这种方式向您提供数据,您不能用它做很多事情(并且必须自己获取哈希),但只要两个密钥相同,第二个密码值就会覆盖第一个。

    你是正确的假设可以使用 strtolower 函数来确保用户名不区分大小写,只需在尝试用户名和用户数据的用户名进行比较时都使用它

    strtolower($loginInfo[$attempt][0]) === strtolower($userData['UserName'])
    

    md5 将用于获取密码的哈希值,以便您可以根据需要使用它,例如

    $hash = md5($loginInfo[$attempt][1];
    

    substr 将帮助您获取 md5 哈希的最后 8 个字符,该函数的使用如下:

    string substr ( string $string , int $start [, int $length ] )
    

    不想毁了你所有的功课,但请检查http://php.net/manual/en/function.substr.php,你会在否定的开始示例中找到答案。

    【讨论】:

      【解决方案3】:
      function checkLogin($storage, $input){
        foreach ($storage as $data){
          if ((strtolower($data['Name']) === strtolower($input[0])) && (md5($data['Password']) === md5($input[1])))
            return true ;
        }
        return false ;
      }
      
      $user_logged = checkLogin($userData, $_POST /* Or another source */) ;
      
      var_dump($user_logged) ;
      

      我不知道您为什么需要比较哈希的最后 8 个字符,但如果您愿意,我可以编辑我的答案。

      但如果你还需要它,就改变

      (md5($data['Password']) === md5($input['Password']))
      

      到

      (substr(md5($data['Password']), -7)) === (substr(md5($input['Password']), -7))
      

      尝试登录所有用户:

      $logged_users = array() ;
      foreach ($loginInfo as $info){
        if (checkLogin($userData, $info){
          $logged_users[] = $info ;
        }
      }
      
      var_dump($logged_users) ;
      

      【讨论】:

      • 我需要指出 2 个密码字段实际上不在代码中。实际密码(第二个字段)被注释掉,仅用于向我们显示密码是什么。我不知道这是否有什么不同。
      猜你喜欢
      • 2023-03-03
      • 2011-01-24
      • 1970-01-01
      • 2017-07-16
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      相关资源
      最近更新 更多