2013-05-15 88 views
0

我有一個php登錄腳本(下)出現問題。我想重定向到nouser.php,如果有人輸入了不存在的用戶名,並且如果輸入了錯誤的密碼(但是輸入了有效的用戶名),則輸入wrongpass.php。下面的代碼幾乎可以工作。如果我註釋掉整個錯誤的密碼部分,則nouser部分按預期工作,顯示nouser頁面,但如果我在錯誤的密碼部分中輸入錯誤的密碼部分,則會出現錯誤的pass.php頁面。如果我把一個有效的用戶登入,但密碼錯誤,那麼我得到了錯誤的密碼(正確的行爲)。 簡單地說,我怎麼能保證我得到重定向到nouser.php如果有這個名字的nouser而不是wrongpass.php網頁..PHP登錄檢查流程

<?php 
      $username = $_POST['username']; 
$password = $_POST['password']; 
//connect to the database here 

require_once 'includes/login.php'; 
$db_server = mysql_connect($db_hostname, $db_username, $db_password); 
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); 

mysql_select_db($db_database, $db_server) 
    or die("Unable to select database: " . mysql_error()); 


$username = mysql_real_escape_string($username); 
$query = "SELECT password, salt 
     FROM users 
     WHERE username = '$username';"; 
$result = mysql_query($query); 
//wrong user section 
if(mysql_num_rows($result) < 1) //no such user exists 
{ 
    header('Location: nouser.php'); 
} 
//wrong password section 
$userData = mysql_fetch_array($result, MYSQL_ASSOC); 
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password)); 
if($hash != $userData['password']) //incorrect password 
{ 
    header('Location: wrongpass.php'); 
} 
//login successful 


?> 
+0

值得注意的是,mqsql_已被棄用。你應該改用mysqli。我最近做了這個開關,實際上mysqli的OO特性(你可以但不必使用)好得多 – o0rebelious0o

+1

還值得注意的是,你通過給出兩個目標腳本來輸入nouser和錯誤的密碼。抓住我的意思? – Drew

+0

你只應該建議所提供的用戶名和密碼組合是無效的,否則你告訴人們他們已經猜到了一個有效的用戶名,現在他們只需猜測密碼。 – Anigel

回答

1

我想你應該添加模具()停止腳本

if(mysql_num_rows($result) < 1) { 
    header('location:nouser.php'); 
    die(); 
} 

尚未測試代碼。

+0

,似乎工程謝謝。我想過但認爲它可能會破壞別的東西。應該真的嘗試過。 – boliviab

+0

是呀破解退出救援 – Drew

+0

@ user2332903但我認爲你不應該創建自己的login.php許多身份驗證插件將幫助你更安全。 – egon12