我有一個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
?>
值得注意的是,mqsql_已被棄用。你應該改用mysqli。我最近做了這個開關,實際上mysqli的OO特性(你可以但不必使用)好得多 – o0rebelious0o
還值得注意的是,你通過給出兩個目標腳本來輸入nouser和錯誤的密碼。抓住我的意思? – Drew
你只應該建議所提供的用戶名和密碼組合是無效的,否則你告訴人們他們已經猜到了一個有效的用戶名,現在他們只需猜測密碼。 – Anigel