2017-06-18 129 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

你能告訴我們你的帳戶表結構和你得到 – Dale

+1

你的腳本是[** SQL注入攻擊的危險性錯誤**](https://stackoverflow.com/q/60174/5914775)。看看[Little Bobby Tables]發生了什麼事(http://bobby-tables.com/)。改用[準備好的參數化語句](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變種。 你需要檢查並在必要時對其進行初始化 - 這樣的事情:

if (!isset($_SESSION['username'])) { 
    if (!is_array($_SESSION)) { 
     $_SESSION = []; 
    } 
    $_SESSION['username'] = $username; 
}