2014-01-06 347 views
0

在此先感謝您的幫助。嘗試使用php編程使用用戶名和密碼進行基本登錄。用html提交按鈕創建了帶有用戶名和密碼輸入框的基本表單,並且還設置了phpmyadmin數據庫。用戶名/密碼登錄PHP故障

已經在表中輸入了兩個條目,並且已經確認php中的mysql連接正常工作並連接到數據庫。但是當我嘗試在表單中輸入正確的用戶名和密碼時,我不斷地收到「不正確的密碼」而不是「你在」。

任何人都可以告訴我,我的編碼是關閉的嗎? (對不起格式不好,我正在使用有趣的文本編輯器)

<?php 

$username = $_POST['username']; 
$password = $_POST['password']; 

$domainname  = 'mydomainname'; 
$domainpassword = 'mypassword'; 
$hostname  = 'localhost'; 
$database  = 'mydomainname_...phplogin'; 

if($username && $password) { 

    mysql_connect($hostname, $domainname, $domainpassword) or die("Couldn't Connect!"); 
    @mysql_select_db($database) or die("Unable to select database"); 

    $query = mysql_query("SELECT * FROM users WHERE username='$username'"); 

    $numrows = mysql_num_rows($query); 

    if ($numrows!=0) {  
     while ($row = mysql_fetch_assoc($query)) {  
      $dbusername = $row['username']; 
      $dbpassword = $row['password'];  
     } 

     if ($username==$dbusername&&$password==$dbpassword){   
      echo "You're in!";  
     } else { 
      echo "Incorrect Password!"; 
     } 

    } else { 
     die("I'm afraid there isn't a user with that name!"); 
    } 

} else { 
    die("Please enter an actual username and a password!"); 
} 

?> 
+0

您是否驗證了您的數據庫中包含相同的值? (順便說一下,MySQL擴展已被棄用,切換到MySQLi或PDO,如果你繼續使用MySQL,至少使用'mysql_real_escape_string'來轉義輸入值 – ComFreek

+1

如果我輸入用戶名''或用戶名LIKE'%'什麼發生? 我建議您查看SQL注入的代碼,使用適當的MySQLi或PDO庫,並使用適當的行縮進,括號和\例外 – DanFromGermany

+3

改善您的代碼除了不受保護的查詢和不推薦使用的mysql,您不應該將密碼存儲在明文 –

回答

0

您的SQL查詢出現問題。

我編輯了自己的代碼..

<?php 

$username = $_POST['username']; 
$password = $_POST['password']; 

$domainname  = 'mydomainname'; 
$domainpassword = 'mypassword'; 
$hostname  = 'localhost'; 
$database  = 'mydomainname_...phplogin'; 

if($username && $password) { 

mysql_connect($hostname, $domainname, $domainpassword) or die("Couldn't Connect!"); 
@mysql_select_db($database) or die("Unable to select database"); 

$query = mysql_query("SELECT * FROM users WHERE username='".$username."'"); 

$numrows = mysql_num_rows($query); 

if ($numrows!=0) {  
    while ($row = mysql_fetch_assoc($query)) {  
     $dbusername = $row['username']; 
     $dbpassword = $row['password'];  
    } 

    if ($username==$dbusername&&$password==$dbpassword){   
     echo "You're in!";  
    } else { 
     echo "Incorrect Password!"; 
    } 

} else { 
    die("I'm afraid there isn't a user with that name!"); 
} 

} else { 
die("Please enter an actual username and a password!"); 
} 

?>