2016-12-22 11 views
1

phpとmysqlデータベースを使用してログインフォームを取得しようとしています。 この瞬間、フォームは何もしていないようです。 投稿をクリックすると、ログインフォームなしでログインページが再読み込みされます。PHP PDOログインシステム

<?php 
 

 
require_once 'header.php'; 
 
$current = 'login.php'; 
 
require_once 'navigatie.php'; 
 

 

 
if(isset($_POST['login'])){ 
 

 
    $username = $_POST["gebruiker"]; 
 
    $password = $_POST["psw"]; 
 

 
    try{ 
 
     $stmt = $db->prepare("SELECT * FROM gebruiker WHERE (naam = :gebruiker AND wachtwoord = :psw)"); 
 
      $stmt ->bindParam(":naam", $username); 
 
     $stmt ->bindParam(":psw", $password); 
 
     $stmt ->execute(); 
 
     $db = null; 
 
     $result=$stmt->fetch(); 
 
     if($stmt->rowCount() == 1) 
 
     { 
 
      //gebruiker bestaat 
 
      header("location: index.php"); 
 
      session_start(); 
 
     } 
 
     else{ 
 
      echo "gebruiker bestaat niet"; 
 
     } 
 

 
    } 
 
    catch (PDOException $e){ 
 
     $message = $e . "Failed"; 
 
    } 
 

 
} 
 
?> 
 

 
<main> 
 

 
    <div class="row"> 
 
    <form role="form" class="col s12" method="POST" action="login.php" > 
 
     <div class="row"> 
 
      <div class="container"> 
 

 
       <div class="input-field col s6"> 
 
        <i class="material-icons prefix">face</i> 
 
        <input name="gebruiker" id="icon_prefix" type="text" class="validate"> 
 
        <label for="icon_prefix">Naam</label> 
 
       </div> 
 

 

 
       <div class="input-field col s6"> 
 
        <i class="material-icons prefix">lock</i> 
 
        <input name="psw" id="icon_psw" type="password" class="validate"> 
 
        <label for="icon_psw">password</label> 
 
       </div> 
 

 
       <div class="col s12" > 
 
        <button id="submit" class="btn waves-effect waves-light" type="submit" name="login">Login 
 
         <i class="material-icons right">send</i> 
 
        </button> 
 

 
       </div> 
 
      </div> 
 
    </form> 
 
    </div>

+0

という別のconnection.phpはこの回答に含まれていません。 :gebruiker "、$ username); ' –

+1

一般的なセキュリティ警告:データベースにユーザーパスワードを保存することはありません。決して。パスワードの何が格納されているかは_hash_です。次に、認証時に、提供されたパスワードをもう一度ハッシュし、_は2つのハッシュを比較します。そうすれば、システムが侵害されてもユーザーのパスワードが失われることはありません。 – arkascha

+0

私はこれがケープタウン出身のアフリカ系アメリカ人の男だと思った。 –

答えて

0

君たちの助けを借りて、私の先生の私はsoullotionを発見しました。私はデータベースを修正し、コードには余分なコード行があります。しかし、私はこれを投稿して他の学生やコーダーに役立てています。

<?php 
 
session_start(); 
 
require_once 'header.php'; 
 
$current = 'login.php'; 
 
require_once 'navigatie.php'; 
 
$errMsg =""; 
 

 
if(isset($_POST['login'])){ 
 
    require_once "connectie.php"; 
 
    $username = $_POST["username"]; 
 
    $password = $_POST["password"]; 
 

 
    if($username == '') 
 
     $errMsg = "geen gebruiker gedefineerd"; 
 

 
    if($password == '') 
 
     $errMsg = "Geen wachtwoord"; 
 
    if ($errMsg == ''){ 
 

 
     $stmt = $db->prepare("SELECT id,username,password FROM gebruiker WHERE username =:username AND password =:password"); 
 
     $stmt ->bindParam(':username',$username); 
 
     $stmt ->bindParam(':password', $password); 
 
     $stmt->execute(); 
 
     $results = $stmt->fetch(); 
 
     if($stmt->rowCount() == 1){ 
 
      $_SESSION['username'] = $username; 
 
      echo "<script>window.location = 'index.php'</script>"; 
 
     }else{ 
 
      $errMsg = "Gebruiker en/of wachtwoord niet gevonden"; 
 
     } 
 
    } 
 
} 
 
?> 
 

 
<main> 
 
    <?php echo $errMsg;?> 
 
    <div class="row"> 
 

 
    <form role="form" class="col s12" method="POST" action="login.php" > 
 
     <div class="row"> 
 

 
      <div class="container"> 
 

 
       <div class="input-field col s6"> 
 
        <i class="material-icons prefix">face</i> 
 
        <input name="username" id="icon_prefix" type="text" class="validate"> 
 
        <label for="icon_prefix">Naam</label> 
 
       </div> 
 

 

 
       <div class="input-field col s6"> 
 
        <i class="material-icons prefix">lock</i> 
 
        <input name="password" id="icon_psw" type="password" class="validate"> 
 
        <label for="icon_psw">password</label> 
 
       </div> 
 

 
       <div class="col s12" > 
 
        <button id="submit" class="btn waves-effect waves-light" type="submit" name="login">Login 
 
         <i class="material-icons right">send</i> 
 
        </button> 
 

 
       </div> 
 
      </div> 
 
    </form> 
 
    </div> 
 

 

 

 

 
</main> 
 
<?php require_once 'footer.php'; ?>

私はナームからユーザ名とwachtwoordへのパスワードに、データベース内の次のテーブルを変更しました。 $ stmt - > bindParam( ":naam"、$ username); `write` $ stmt - > bindParam( "$ stmt - > bindParam"の代わりに

2

あり、以下にこの2行worng何か:

データベースの構造:以下のように

Database structure

コードです

:naamという名前のパラメータをバインドしようとしましたが、この単語はSQL文の列のようです。スクリプトを分析した後とgebruiker assumig

は、POST要求から受信している、これらの行は次のようになります。

まあ
$stmt = $db->prepare("SELECT * FROM gebruiker WHERE (naam = :gebruiker AND wachtwoord = :psw)"); 
$stmt ->bindParam(":gebruiker", $username); //<-- Look at Param name binded 
+0

それが正しい場合(そうであれば、ログインしている場合)、セッションを開始してindex.phpにリダイレクトする必要があります。 –

+0

これらの変更後にエラーが発生しましたか?もしそうなら、直面しているすべてのエラーを投稿してください。 – bcesars