0

私は何か特定のことをしようとしています。配列の各要素のユーザー名とパスワードを比較して、既存のユーザーと一致するものを見つける必要があります。

2 つの配列があります。すべてのユーザー情報を含む 1 つ。もう 1 つはログイン試行を含むものです。演習は、ログイン試行が一致した場合にユーザー情報を出力することです。したがって、$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);
?>
4

3 に答える 3

0

ユーザー データ配列に 2 つの「パスワード」フィールドがあることに気付きました。最初のパスワード フィールドは、2 番目のパスワード フィールドの md5 ハッシュの最後の 8 文字のように見えます。データがこの方法で提供された場合、多くのことはできません。それは(そして自分でハッシュを取得する必要があります)が、両方のキーが同じである限り、2 番目のパスワード値が最初の値を上書きします。

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を確認してください。否定的な開始例で答えが見つかります。

于 2013-02-19T12:24:12.480 に答える
0
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) ;
于 2013-02-19T12:05:24.333 に答える
0

2 つの配列の値を比較するには、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);
            }
        }
    }
}
于 2013-02-19T12:03:05.220 に答える