2017-04-18 100 views
1

我是PHP的初學者,所以這個登錄腳本會有很多安全漏洞。不過不要擔心。一旦我得到修復,我會得到修復。這是我的登錄PHP腳本。PHP登錄腳本未選擇行

<?php 
$error=''; 
if(isset($_POST['submit'])){ 
    if(empty($_POST['username']) || empty($_POST['password'])) { 
     $error = "Username or Password is Invalid"; 
     echo "<script type='text/javascript'>alert('$error');</script>"; 
    } 
    else { 
     $user = $_POST['username']; 
     $pass = $_POST['password']; 

     $servername = "localhost"; 
     $username = "id1394453_users"; 
     $password = "password"; 
     $database = "id1394453_users"; 

     $conn = mysqli_connect($servername, $username, $password, $database); 

     if (!$conn) { 
      die("Connection failed: " . mysqli_connect_error()); 
     } 

     $query = mysqli_query($conn, "SELECT * FROM Users WHERE user='username' AND pass='password'"); 

     $rows = mysqli_query($conn, $query); 
     if (mysqli_num_rows($rows) == 1) { 
      header("Location: Home.html"); 
     } 
     else { 
      $error = "Username of Password is Invalid"; 
      echo "<script type='text/javascript'>alert('$error');</script>"; 
      echo "<script type='text/javascript'>alert('$user');</script>"; 
      echo "<script type='text/javascript'>alert('$pass');</script>"; 
      echo "<script type='text/javascript'>alert('$rows');</script>"; 
     } 
     mysqli_close($conn); 
    } 
} 

?>

我已經檢查,以確保它連接到MySQL數據庫和所有的變量是正確的。當它到達線26

如果(mysqli_num_rows($行)== 1){

它需要的其他路徑。這不是我想要的。正如你所看到的,我檢查了$ rows是什麼值,它什麼也沒有。因此,我將注意力轉移到第25行

$ rows = mysqli_query($ conn,$ query);

我已經在測試$ conn以確保連接正常工作。所以這使我想到這是在第23行

$查詢= mysqli_query定義的$查詢veriable($康恩, 「SELECT * FROM用戶WHERE USER = '用戶名' 並通過= '密碼'」);

這是我認爲有麻煩的地方。問題是我不確定它有什麼問題。該表被稱爲「用戶」,我想要的列被稱爲「用戶名」和「密碼」。嗯是的。我在這裏做錯了什麼?

+1

; *是否/將它?* – nogad

+1

我希望這件事不是去現場 –

回答

2

這裏的查詢看起來是靜態的。

$query = mysqli_query($conn, "SELECT * FROM Users WHERE user='username' AND pass='password'"); 

你應該用變量代替它吧?您忘記了$

$query = mysqli_query($conn, "SELECT * FROM Users WHERE user='$user' AND pass='$pass'"); 

此外,還有一個錯誤。 $query應該是$rows。你應該在這裏收到一個錯誤:

$query = mysqli_query($conn, "SELECT * FROM Users WHERE user='username' AND pass='password'"); 
$rows = mysqli_query($conn, $query); 

以上是完全無效的。它應改爲:

$query = "SELECT * FROM Users WHERE username='$user' AND password='$pass'"; 
$rows = mysqli_query($conn, $query); 

而且不要忘了SQL注入防護部:

$user = mysqli_real_escape_string($conn, $_POST['username']); 
$pass = mysqli_real_escape_string($conn, $_POST['password']); 
運行查詢兩次
+0

他使用'$ user'和'$ pass' – nogad

+0

@ thebootsie123否否...檢查更新後的答案。 –

+0

@nogad請檢查問題! ':'' –