2016-06-29 1 views
0

私はログインページでウェブサイトを構築しています。示され、htmlファイルは以下であると、ログインページは、HTMLフォームがありますMySQLi query - php - html form:HTMLフォームから取得したクエリ行は、ハードコード化されたクエリ行と異なりますか?

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" /> 
    <title>Login Page</title> 
    <link rel="stylesheet" type="text/css" href="/my-site/LoginPage/loginPageGraphics.css"/> 
</head> 
<body> 
    <form name="frmLogin" method="post" action="/my-site/LoginPage/login.php" accept-charset="UTF-8"/> 
     <div id="loginField"> 
      <div> 
       <script type="text/javascript" src="/my-site/LoginPage/loginPageAction.js"></script> 
       <script type="text/javascript" src="/my-site/database/databaseActions.js"></script> 
       <p> 
        <input id="txtUsername" name="username" type="text" placeholder="username"/> <br/> 
        <input id="txtPassword" name="password" type="password" placeholder="password"/> <br/> 
        <input id="btnLogin" type="submit" value="Login"/> 
        <input type="hidden" name="query" value="SELECT ID, Fname, Lname, Username, Password, Type FROM user_accounts WHERE Username='$Username'"/> 
       </p> 
      </div> 
      <div> <!--This div block has no code behind it yet. It is just there for now.--> 
       <p> 
        <input id="btnCreateAccount" type="submit" value="Create Account" onclick="createAccount(txtUsername.value, txtPassword.value)"/> <br/> 
        Create admin account? <input id="chbCreateAdmin" type="checkbox"/> 
       </p> 
      </div> 
     </div> 
    </form> 
</body> 
</html> 

送信ボタンbtnLoginをクリックすると、フォームが

<?php //this is login.php 
$Query = $_POST["query"]; //Query line from html form 
$Username = $_POST["username"]; 

//This 'hard-coded' query is the exact same as the query line from html form 
$Query1 = "SELECT ID, Fname, Lname, Username, Password, Type FROM user_accounts WHERE Username='$Username'"; //hard-coded query line 
echo $Query;//There is only 1 difference between these two echos. This echo displays "... Username='$Username'"; //$Username is the variable that holds the entered user name 
echo $Query1;//            While this echo displays "... Username='TylerB'"; //TylerB is the entered username 
if($Query === $Query1){ 
    echo "true"; 
}elseif($Query == $Query1){ 
    echo"true2"; 
}else{ 
    echo"false"; //this one gets echoed 
} 

require "../database/dbConnection.php"; //the contents of this file is below. I am using require, as I want this php file to be re-usable. 
/* |___> require "DB.php"; //The DB.php file holds the database login info 

      $conn = new mysqli($host, $user, $pass, $dbName); //Variables specified in DB.php 
      if($conn->connect_error){ 
       die("Connection failed: " . $conn->connect_error); 
      } 
      $result = $conn->query($Query); 
*/ 

/*$Query: $result has 0 num_rows 
$Query1: $result has 1 num_rows (this is expected) 
This information was found via var_dump($results); The lines of codes was removed a little while ago*/ 
if($result->num_rows > 0){ //If num_rows is equal to 1, no account with the entered username was found 
    while($row = $result->fetch_assoc()){ 
     if($_POST["password"] === $row["Password"]){//If entered password matchs the selected account's password, login to home page 
      if($row["Type"] === "Admin"){//Purpose of this if statement is not relevant to the question. Just know, if login info is correct, user logins to a home page. 
       echo "go to admin home page"; 
      }else{ 
       echo "go to student home page"; 
      } 
     }else{//if password is incorrect 
      echo "ERROR: Username or password was incorrect. Please enter the correct account information."; 
     } 
    } 
}else{//if username is incorrect 
    echo "ERROR: Username or password was incorrect. Please enter the correct account information."; 
} 
?> 

ときI login.php、PHPスクリプトを実行します変数$ Query1を使用すると、Webサイトが正しく実行されます。ただし、$ Queryを使用すると、$ resultのnum_rowsは0になります。つまり、ユーザー名がTylerBのアカウントが見つかりませんでした($ Query1にはユーザー名がTylerBのアカウントがありますが)。

クエリ行の末尾に$ Query1をと$クエリの間でエコー表示の唯一の違い:(これらの二つの変数についての詳細はlogin.phpファイルの先頭を参照してください)

echo $Query;//There is only 1 difference between these two echos. This echo displays "... Username='$Username'"; //$Username is the variable that holds the entered user name 
echo $Query1;//            While this echo displays "... Username='TylerB'"; //TylerB is the entered username 

私の質問..

なぜ2つのクエリ変数が2つの異なるクエリ結果を与えるのですか?これをどうやって解決するのですか?

これは、主に、$ Queryと$ Query1の2つのわずかに異なるエコーの周りを回転していると思われます。 $ Usernameは "... Username = '$ Username'"の代わりに$ Usernameの値の代わりにクエリされますか? (私はこれを自分で疑う...)

もしそうなら、どうすればよいでしょうか? dbConnection.phpが要求するクエリ行は、別のソースから取得する必要があります。それ以外の場合は、ログインページに固有のものになります。これは私の意図とは正反対です。クエリーが必要なときにdbConnection.phpを再利用できるようにします。

バージョン/プログラムなど:

崇高3

Apache Webサーバ(http://www.wampserver.com/en/使用しWAMPSERVER(64 BITS & PHP 5.6.15 $ PHP 7)

PHP 7

から1つのパッケージ内のすべての

Google Chromeの

MySQLの

JS

CSS

のWindows 10

------------------------------- -------------- EDIT -------------------------------

私が行っていたほどの再利用可能なコードはありませんが。私はまだ私が持っているもので動作することができます。しかし、安全性とハッキングに関するすべてのコメントに関して...このコードはどれくらい安全ですか?

<form name="frmLogin" method="post" action="/my-site/LoginPage/login.php" accept-charset="UTF-8"/> 
     <div id="loginField"> 
      <div> 
       <script type="text/javascript" src="/my-site/LoginPage/loginPageAction.js"></script> 
       <script type="text/javascript" src="/my-site/database/databaseActions.js"></script> 
       <p> 
        <input id="txtUsername" name="username" type="text" placeholder="username"/> <br/> 
        <input id="txtPassword" name="password" type="password" placeholder="password"/> <br/> 
        <input id="btnLogin" type="submit" value="Login"/> 
       </p> 
      </div> 
      <div> 
       <p> 
        <input id="btnCreateAccount" type="submit" value="Create Account" onclick="createAccount(txtUsername.value, txtPassword.value)"/> <br/> 
        Create admin account? <input id="chbCreateAdmin" type="checkbox"/> 
       </p> 
      </div> 
     </div> 
    </form> 

送信は以下を実行します...

<?php 
$User = $_POST["username"]; 

require "../database/dbConnection.php"; 
/* | 
<?php 
    require "DB.php"; 
    $conn = new mysqli($host, $user, $pass, $dbName); 
    if($conn->connect_error){ 
     die("Connection failed: " . $conn->connect_error); 
    } 
?>*/ 
$stmt = $conn->prepare("SELECT ID, Fname, Lname, Username, Password, Type FROM user_accounts WHERE Username=?"); 
$stmt->bind_param("s", $User); 
$stmt->execute(); 
$stmt->bind_result($ID, $Fname, $Lname, $Username, $Password, $Type); 
$stmt->fetch(); 

if($User === $Username){ 
    if($_POST["password"] === $Password){ 
     if($Type === "Admin"){ 
      echo "go to admin home page"; 
     }else{ 
      echo "go to student home page"; 
     } 
    }else{ 
     echo "ERROR: Username or password was incorrect. Please enter the correct account information."; 
    } 
}else{ 
    echo "ERROR: Username or password was incorrect. Please enter the correct account information."; 
} 
?> 
+0

あなたは、 'FNAMEの値=" IDを選択を使用している理由は、私は思ったんだけど、 Lname、ユーザー名、パスワード、タイプFROM user_accounts WHERE Username = '$ Username' "'あなたのHTMLソースを見て、どこでもエラーをチェックしてください。 PHPとMySQL経由です。 –

+0

そのハードコーディングされた行はテストのためのものでした。私はdbConnection.phpを再利用可能にしたい。私は、クエリが必要なときはいつでも、get/postと$ _POSTを使ってPHPファイルに情報を渡すことができるフォームが存在すると信じています。だから、私はクエリラインで隠された入力を持つことにしました。しかし、私は問題に遭遇し、それが私に$ Query1 = query lineを使用させました。私はプログラムで何が起こっているかをもっと知ることができます。 – Tyler

+1

SQLクエリーをフォームに表示させることは非常に悪いことですが、バーン・ドアのような大きなセキュリティ・ホールを開くことになります。ただしないでください。 –

答えて

関連する問題