2017-01-20 9 views
0

私はユーザー名とパスワードを受け入れ、file.phpに送る基本的なフォームを持っていました。私が比較しているところでは、usernameとpasswordは既にpassword.txtにあるものと比較されます。htmlのPHPファイルに設定するとターゲットの場所が空白になる

HTML:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Authentication</title> 
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"> 
<link rel="stylesheet" type="text/css" href="css/styles.css"> 
</head> 

<body> 
    <div class="signin text-center"> 
     <form action="file.php" method="POST"> 
      <div class="form">Username: 
      <input type="text" name="username"></div> 

      <div class="form">Password: 
      <input type="Password" name="password"></div> 

      <input type="submit" value="Go"> 
     </form> 
    </div> 

</body> 
</html> 

PHP:

<html> 
    <head> 
    <title>Password test, page 2</title> 
    <head> 


    <body> 

     <?php 
     $name = $_POST['username']; 
     $pass = $_POST['password']; 

     //read the contents of our password file. 
     $myFile = "password.txt"; 
     $fh = fopen($myFile, 'r'); 
     $data = fgets($fh); 
     fclose($fh); 

     //echo the data from the text file 
     echo $data; 

     //print out an HTML line break 
     print "<br>"; 

     //now we need to split our line of data from the text 
     //file so that we can do the comparison. 

     //split the text into an array 
     $text = explode(":",$data); 


     //echo the split user name 
     echo $text[0]; 

     //print out an HTML line break 
     print "<br>"; 

     //echo the split password 
     echo $text[1]; 

     //assign the data to variables 
     $good_name = $text[0]; 
     $good_pass = $text[1]; 

     //print the variables 
     echo $good_name; 
     echo $good_pass; 

     //print out an HTML line break 
     print "<br>"; 

     //compare the strings 
     if($name === $good_name && $pass === $good_pass){ 
      echo "That is the correct log-in information"; 
     }else{ 
      echo "That is not the correct log-in information."; 
     } 
     ?>  
     <br> 

    </body> 
</html> 

PASSWORD.TXTは 含まれています。私はKなどとパスワードとしてユーザー名を入力すると、それは私を与える

k個誤った出力、すなわちログインが正しくないしかし、私はパスワードを確認するための条件を削除する場合、それは私に正しいログインを与える。

フォームのパスワードが暗号化されているため、文字列と比較できない可能性があります。しかし、私は、HTMLフォームはパスワードを暗号化しないと考えています。

また、私は自分のコンピュータからこのサイトをホストしています。

ご協力いただきありがとうございます。ありがとう

+1

私は、ある時点でこれでライブに行くつもりはないと思っています。 –

+0

また、 'password.txt'に含まれるものの例を提供する必要があります。 –

+0

ライブ公開しません。そうでなければ私のサイトは今何も構成されていません。 password.textには次のような単純な行が含まれています:kそれだけです。 –

答えて

1

あなたは間違ったものでmd5をやっています。 ファイルにmd5文字列としてパスワードを保存しておく必要があります。また、POST経由で受信するときは、md5($_POST['pass'])を実行する必要があります。それを比較すればOKです。空白を削除して、改行記号がtrim()であることを確認していない場合。

例:

<?php 
     $name = $_POST['username']; 
     $pass = md5($_POST['password']); // add md5 here 

     //assign the data to variables 
     $good_name = $text[0]; 
     $good_pass = $text[1]; // remove md5 here as it should be already encoded with it 
    // ... 

     //compare the strings 
     if($name === $good_name && $pass === $good_pass){ 
      echo "That is the correct log-in information"; 
     }else{ 
      echo "That is not the correct log-in information."; 
     } 

編集#1:txtファイルの 例:

admin:21232f297a57a5a743894a0e4a801fc3 

パスワードは次のとおりです。admin

+0

テキスト[1]は単純なテキストなので、私自身がそこに置くように暗号化されたmd5ではありません。基本的には、good_nameはプレーンテキスト、passはプレーンテキストです。私はどこが間違っているのか混乱しています。なぜならどこからでもmd5を削除しても、それは私が望む方法ではまだ動作しないからです。 –

+0

@AkritiAnandそれはあなたですなぜあなたはmd5を使うべきか分かりません。テキストファイルにエンコードされたパスワードを保存する必要があります。私はtxtファイルの例で分の編集を追加します。 – malutki5200

+0

これは暗号化したくないと思います。私はプレーンテキストのみを比較したい。これを許可しないフォームのこのパスワードフィールドには何が入っていますか?私はこれに新しいので、これは少しばかげている場合は申し訳ありません。 –

0

解決策ではなく、シンプルで私の間違いだったようですむしろ恥ずかしいです。私はパスワード入力フィールドのタイプを書いている間にタイプミスをしました。 「パスワード」ではなく「パスワード」でなければなりません。今それは動作します。みんなあなたを助けてくれてありがとう。

関連する問題