2017-06-18 8 views
-1

私はPHPとのログインセッションをしようとしていると私は私のデータベースの "ユーザー名"フィールドで受信し、エラーが発生します。 私のソースコードはここにある:PHPの通知:未定義のインデックス:ユーザー名

のindex.php

<?php 
session_start(); 
if(isset($_POST['bttLogin'])){ 
    require 'connect.php'; 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $result = mysqli_query($con, 'SELECT * FROM account WHERE username=" '.$username.' " AND password="'.$password.' " '); 
    if (mysqli_num_rows($result)==1){ 
     $_SESSION['username'] = $username; 
     header('Location: welcome.php'); 
    } 
    else 
     echo "Account invalid"; 
} 
if(isset($_POST['username'])){ 
    $filename = $_POST['username']; 
} 

if (isset($_GET['logout'])){ 
    session_unregister('username'); 
} 

?> 
<form method="post"> 

    <table cellpadding="2" cellspacing="2" border="0"> 
     <tr> 
      <td> Username </td> 
      <td><input type = "text" name="usermame"></td> 
     </tr> 

     <tr> 
      <td> Password </td> 
      <td><input type = "password" name="password"></td> 
     </tr> 

     <tr> 
      <td> </td> 
      <td><input type = "submit" name="bttLogin" value="Login"></td> 
     </tr> 
    </table> 
</form> 

私は自分のデータベースと私の接続をしたが、それは、ユーザー名が未定義であることを言って続けています。助けてください。

+0

はあなたが私達にあなたのアカウントのテーブル構造とあなたはあなたのスクリプトは、[** SQLインジェクション攻撃の危険にさらされている – Dale

+1

を取得しているエラーを示すことができました**](https://stackoverflow.com/q/60174/5914775)。 [Little Bobby Tables](http://bobby-tables.com/)に何が起こったのか見てみましょう。代わりに、[prepared parameterized statements](https://php.net/manual/en/mysqli.quickstart.prepared-statements.php)を使用してください。 –

+1

**プレーンテキストパスワードは絶対に保存しないでください!** PHPは['password_hash()'](https://php.net/manual/en/function.password-hash.php)と['password_verify()']を提供していますhttps://php.net/manual/en/function.password-verify.php)それらを使用してください。 5.5より前のバージョンのPHPを使用している場合([互換性パックがあります])(https://github.com/ircmaxell/password_compat)ハッシュする前に[**パスワードをエスケープしない**](https://stackoverflow.com/q/36628418/5914775)を確認するか、他のクレンジングメカニズムを使用してください。これを行うと、パスワードが変更され、不要な追加のコーディングが発生します。 –

答えて

0

$ _SESSION varにインデックス 'username'がありません。 かを確認し、必要に応じてそれを初期化する必要がある - このような何かを:

if (!isset($_SESSION['username'])) { 
    if (!is_array($_SESSION)) { 
     $_SESSION = []; 
    } 
    $_SESSION['username'] = $username; 
} 
関連する問題