2011-06-21 44 views
1

Facts:登錄用戶,避免取消用戶帳戶?

  1. 我們系統上的用戶可以通過他們的電子郵件地址和密碼登錄。
  2. 電子郵件地址是唯一的標識符,但如果accoutn被取消,另一個人可以使用相同的電子郵件地址註冊。以前的記錄仍然在我們的系統中。
  3. 如果被取消的用戶試圖登錄,我們不應該允許他訪問。

現在,當在登錄用戶時檢查記錄是否存在時,是否正確執行以下操作?

$rs=$DBobject->execute_query("select * from users where `email`='".$email."' and `password`='".passwordMD5($password)."' and status!='cancelled'"); 
if($DBobject->my_num_rows($rs)>0) 
{ 
    // login the user 
} 
else 
{ 
    // check if account has been cancelled 
    $rs_cancelled=$DBobject->execute_query("select id from users where `email`='".$email."' and `password`='".passwordMD5($password)."' and status='cancelled'"); 
    if($DBobject->my_num_rows($rs_cancelled)>0) 
    { 
     return "Your account has been cancelled"; 
    } 
    else 
    { 
     return "Invalid email/password"; 
    } 
} 

我特別關注的事實是,我們正在使用的密碼,以唯一標識記錄($rs_cancelled) - 可以在用於不可告人的目的?上述過程如何被顛覆?

+0

我並不完全明白你的意思是'使用密碼來唯一標識記錄'。你能否詳細說明一下。 – Balanivash

回答

2

除非進入您的查詢的變量在我們無法看到的地方被清理過,否則任何小孩都可以在幾分鐘內破壞這些變量,其中包括SQL injectiontool

始終將轉義變量放入查詢之前。既然你正在使用一些數據庫包裝(一個自定義的?),我不能告訴你做到這一點的確切方式。通常(例如,如果使用PDO)它涉及參數化的準備好的語句。

此外,這段代碼有點浪費。這個怎麼樣:

// WARNING -- WARNING -- HERE PROBABLY BE SQL INJECTIONS 
$rs=$DBobject->execute_query(
    "select * from users where `email`='".$email."' and password`='".passwordMD5($password)."'"); 

// Is there such a user? 
if($DBobject->my_num_rows($rs) == 0) { 
    return "Invalid email/password"; 
} 

// Now fetch the record from $rs -- I don't know how your code does this, 
// but let's assume: 
$row = $rs->getRow(); 

// Now check if the user is cancelled 
if ($row['status'] == 'cancelled') { 
    return "Your account has been cancelled"; 
} 

// Login the user, since the account exists and is not cancelled 

這樣你只需要擊中db一次成功登錄。

0

您並未使用只有用於驗證的密碼。從技術上講,其他用戶不能使用已經使用和取消的電子郵件地址創建帳戶,因爲他無法確認電子郵件地址。我會通過在電子郵件字段上添加一個UNIQUE約束來解決這個問題 - 這可以消除您的潛在問題

0

如果您使用電子郵件識別用戶,並且某些用戶帳戶被取消,則應該刪除用戶記錄以允許用戶使用相同的電子郵件再次註冊,或者不應允許電子郵件再次註冊。