2016-03-21 22 views
2
<?php 
    require('login_include_connection.php'); 

    if (isset($_POST['btn_confirm'])) { 
     $user_name=$_POST['user_name']; 
     $user_password=sha1($_POST['user_password']); 

     if (empty($user_name) || empty($user_password)) { 
      #This validation works only if user place user name, leaving blank password will still works which is not ok 
      echo "Please make corect inputs!!!"; #Both fields must have inputs 
     } else { 
      $sql="INSERT into user_info (id, user_name, user_password) VALUES (null, '$user_name', '$user_password')"; 
      $result=mysqli_query($conn, $sql); #Proceed with sql query and place record to MySQL database 
     } 
    } 
?> 

<!DOCTYPE html> 
<html> 
    <head> 
    <title></title> 
    <style type="text/css"> 
     body { 
     background-color: lightblue; 
     } 
    </style> 
    </head> 
    <body> 
    <h2>User registration</h2> 
    <form method="POST" action="sesija_registracija.php"> 
     <input type="text" name="user_name"> 
     <input type="password" name="user_password"> 
     <input type="submit" name="btn_confirm" value="REGISTER USER NOW"> 
    </form> 
    </body> 
</html> 

前の質問に間違いがあります。したがって、基本的な登録PHPスクリプトがあります。ユーザー名とパスワードを入力する必要があります。パスワードの値がなくても、私のフォームはデータベースにユーザー名を入れます。だから、if文で何が間違っているのですか?empty()sha1()などとは動作しません。私が欲しいのは、ユーザーが両方のフィールドに記入する必要があることを確認することだけです。ハッシュsha1()でのユーザーのパスワード検証は機能しません。

+0

**危険**:不適切なハッシングアルゴリズム(http://php.net/manual/en/faq.passwords.php)を使用しており、[注意を払う]必要があります(https:// www.owasp.org/index.php/Password_Storage_Cheat_Sheet)にユーザーのパスワードを入力します。 – Quentin

+1

**危険**:[SQLインジェクション攻撃](http://bobby-tables.com/)に脆弱** ** [防御]する必要があります(http://stackoverflow.com/questions/ 60174/best-way-to-prevent-sql-injection-in-php)をご利用ください。 – Quentin

+1

挿入の直前のelse節に '$ user_password = sha1($ _ POST ['user_password']);を適用してください。最初の割り当てからsha1を削除します。それはあなたの期待どおりに動作します。 sha256以上を使用してください。[password_hash](http://php.net/manual/en/function.password-hash.php) –

答えて

0
$user_password=sha1($_POST['user_password']); 

POSTされた文字列が空であっても、空でない文字列を返します。あなたの場合の条件の後にパスワードをハッシュする必要が

:また

$user_password = $_POST['user_password']; 
    if (empty($user_name) || empty($user_password)) { 
     #This validation works only if user place user name, leaving blank password will still works which is not ok 
     echo "Please make corect inputs!!!"; #Both fields must have inputs 
    } else { 
     $user_password = sha1($_POST['user_password']); 
     $sql="INSERT into user_info (id, user_name, user_password) VALUES (null, '$user_name', '$user_password')"; 
     $result=mysqli_query($conn, $sql); #Proceed with sql query and place record to MySQL database 
    } 

は、コメントとして、あなたはSQLインジェクションに対して脆弱である、ということに注意してください。

ハッシュも塩漬けしてください。

+0

blue112ありがとうございます、クール –

関連する問題