2011-11-20 70 views
-3

我正在爲我的網站編寫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」。

+6

密碼未被哈希?檢查。用戶輸入不被轉義?檢查。安全明智,這不會變得更糟糕。 – NullUserException

+4

請不要在任何公開顯示的網站上部署此代碼。花一些時間在[PHP安全指南](http://php.net/manual/en/security.php)和[PHP PDO Prepared Statements](http://php.net/manual/en/pdo)上。 prepared-statements.php)指南在打開此頁面供公衆使用之前。 – sarnold

+0

也檢查出這個網站http://www.securityfocus.com/blogs/262。 Google是你的朋友,如果可能的話,使用公開的登錄庫。 – neurotik

回答

0

只是簡單的故障排除是必要的。有多少行返回?返回行的查詢中的userID和passID的值是什麼?休息一下,看看發生了什麼。我沒有看到問題,但它很難排除這裏發佈的代碼,因爲它確實無法在沒有數據庫的情況下運行。

0

我沒有看到任何理由不符合你的預期,我懷疑這個問題可能在別處。例如,我沒有看到您檢查「SES_Admin」會話是否已經註冊。但至少你需要用這個替換第5行和第6行,否則有人可能會刪除你的整個用戶表,並用你的MySQL數據庫做其他各種惡意的事情。

$txtUser = mysql_real_escape_string($_POST['txtUser']); 
$txtPass = mysql_real_escape_string($_POST['txtPass']); 

請務必閱讀正文http://php.net/manual/en/function.mysql-real-escape-string.php

1

這是你的問題是mysql_real_escape_string的文章:

$sqlPassword = "SELECT passID FROM admin WHERE (userID='$txtUser' && passID='$txtPass')"; 
$qryPassword = mysql_query($sqlPeriksa, $sambung); 
$hslPassword = mysql_num_rows($qryPassword); 

你看,你mysql_query正在執行$sqlPeriksa是:

$sqlPeriksa = "SELECT userID FROM admin WHERE userID='$txtUser'"; 

相反,你的代碼應該是像這樣:

$sqlPassword = "SELECT passID FROM admin WHERE (userID='$txtUser' && passID='$txtPass')"; 
$qryPassword = mysql_query($sqlPassword, $sambung); 
$hslPassword = mysql_num_rows($qryPassword); 

請試試這個,讓我們知道發生了什麼。

[編輯/附加]:我強烈建議你考慮以下幾點:

使用PDO:

http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

使用存儲過程:

http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

使用PDO +存儲過程:

http://php.net/manual/en/pdo.prepared-statements.php(請參閱示例#4)

+0

+1非常漂亮! –

+0

哦,是的,你是對的!男人,我怎麼會這麼粗心?謝謝!代碼現在完美無缺地工作(當然,使用mysql_real_escape_string) – user1025536

相關問題