我正在爲我的網站編寫PHP代碼。目前,我的代碼存在一些問題。PHP代碼允許在沒有正確密碼的情況下登錄
這是我的代碼。忽略一些使用的馬來語言,我試圖翻譯其中的大部分。
<?php
session_start();
include "../library/inc.connectiondb.php";
$txtUser = $_POST['txtUser'];
$txtPass = $_POST['txtPass'];
if(trim($txtUser) == "") {
echo "<b>User ID</b> is empty, please fill";
include "login.php";
}
else if(strlen(trim($txtPass)) <= 5) {
echo "<b>Password</b> is less then 6 characters, please fix";
include "login.php";
}
else {
$sqlPeriksa = "SELECT userID FROM admin WHERE userID='$txtUser'";
$qryPeriksa = mysql_query($sqlPeriksa, $sambung);
$hslPeriksa = mysql_num_rows($qryPeriksa);
if($hslPeriksa == 0) {
# If username doesn't exist
echo "<b>UserID</b> doesn't exist";
include "login.php";
}
else {
$sqlPassword = "SELECT passID FROM admin WHERE (userID='$txtUser' && passID='$txtPass')";
$qryPassword = mysql_query($sqlPeriksa, $sambung);
$hslPassword = mysql_num_rows($qryPassword);
if($hslPassword < 1) {
# If password is incorrect
echo "<b>Password</b> is incorrect";
include "login.php";
}
else {
# If login successful
$SES_Admin = $txtUser;
session_register('SES_Admin');
echo "LOGIN SUCCESSFUL";
# Redirect to index.php
echo "<meta http-equiv='refresh' content='0; url=index.php'>";
exit;
}
}
}
?>
問題是這個代碼允許我登錄,即使密碼錯誤。我做了一些搜索,但它仍然不能解決我的問題。我很確定問題出現在第27行。
因此,如果有人有解決方案,請儘快告訴我。我正在爲我的學校編寫此代碼,並且必須在明年之前完成。
編輯 好吧,我已經放在mysql_real_escape_string代碼就像許多人告訴我的。我不知道這將如何幫助,但這個mysql表被命名爲「admin」。它有2個領域;用戶ID和passID。爲了測試代碼,我在表中插入了值「admin」和「12345678」。
密碼未被哈希?檢查。用戶輸入不被轉義?檢查。安全明智,這不會變得更糟糕。 – NullUserException
請不要在任何公開顯示的網站上部署此代碼。花一些時間在[PHP安全指南](http://php.net/manual/en/security.php)和[PHP PDO Prepared Statements](http://php.net/manual/en/pdo)上。 prepared-statements.php)指南在打開此頁面供公衆使用之前。 – sarnold
也檢查出這個網站http://www.securityfocus.com/blogs/262。 Google是你的朋友,如果可能的話,使用公開的登錄庫。 – neurotik