我在開發登錄系統時遇到了問題。我建立了一個處理多個用戶登錄的類。我還爲管理員用戶創建了一個管理員區域。他將能夠添加新用戶,更改密碼,刪除用戶等。更改用戶密碼
現在我設法添加新用戶並更改密碼部分。我現在的問題是關於更改密碼部分。
我有一個表具有這種結構:
TABLE user
id int(11) AUTO INCREMENT
admin int(11) DEFAULT 0
username varchar(256)
password varchar(256)
從表中的管理員定義的用戶類型,0是用於管理用戶誰可以做我在上述正常的用戶和1。
我有以下功能改變的userPassword:
public function changeUserPassword($cusrn,$oldPass,$newPass,$newPassConfirm) {
if(strlen($newPass) < 4) {
$error = true;
$message['error'] = true;
$message['message'] = "The Password is to short";
return json_encode($message);
}
elseif($newPass != $newPassConfirm) {
$error = true;
$message['error'] = true;
$message['message'] = "Confirmed Password doesn't match";
return json_encode($message);
}
elseif(!(ALLOW_SPECIAL_CHARACTERS == true) && preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $newPass)) {
$error = true;
$message['error'] = true;
$message['message'] = "Special characters are not allowed";
return json_encode($message);
}
elseif(!(ALLOW_NUMBERS == true) && preg_match('#\d#',$newPass)) {
$error = true;
$message['error'] = true;
$message['message'] = "Numbers are not allowed";
return json_encode($message);
}
elseif(!isset($error)) {
$oldPass = hash_hmac('sha512',$oldPass,$this -> salt($cusrn,$oldPass));
$newPass = hash_hmac('sha512',$newPass,$this -> salt($cusrn,$newPass));
$sql = 'SELECT username,password FROM user WHERE username = ? AND password = ?';
if($stmt = $this->conn->prepare($sql)) {
$stmt->bind_param('ss',$cusrn,$newPass);
$stmt->execute();
$stmt->bind_result($usrn,$passw);
if($stmt->fetch()) {
if($cusrn == $usrn) {
if(!(strcmp($oldPass,$passw) == false)) {
$error = true;
$message['error'] = true;
$message['message'] = "The current Password is incorrect";
return json_encode($message);
}
elseif (!(strcmp($oldPass,$newPass) == false)) {
$stmt->close();
$sql = "UPDATE user SET password = ? WHERE username = '$usrn' LIMIT 1";
if($stmt = $this->conn->prepare($sql)) {
$stmt->bind_param('s',$newPass);
$stmt->execute();
$stmt->close();
$error = false;
$message['error'] = false;
$message['message'] = "Password successfuly changed";
return json_encode($message);
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "Cannot connect to database for updating";
return json_encode($message);
}
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "New Password must not match the old one";
return json_encode($message);
}
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "The current Username is incorrect";
return json_encode($message);
}
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "Cannot fetch data from the database";
return json_encode($message);
}
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "Cannot prepare database connection";
return json_encode($message);
}
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "Check the PHP syntax | Something went wrong";
return json_encode($message);
}
}
我的問題是,如果我嘗試輸入某個用戶名,要麼拋出我:
"Cannot fetch data from the database"
,或者不斷告訴我:
"The current Username is incorrect"
即使用戶名是正確的。雖然它適用於表中存在的最後一行,但我的意思是它適用於放置在表中最後一行的用戶名。
我敢肯定我做錯了什麼,我在問你可能是什麼錯我在做什麼?
至少嘗試解決所有這些嵌套的'if'的。 – CodeCaster 2011-12-27 22:28:54
你是什麼意思?刪除'如果'並且像那樣工作?這樣我就不知道問題出在哪裏...... – Roland 2011-12-27 22:36:30
幾個快速編碼技巧 - 你的主要if/elseif/else應該縮進到同一級別,它們不是嵌套控件(除非你真的在嵌套它們) 。爲什麼複製$ message ['error']到$ error時,你可以檢查!isset($ message ['error'])。您可以在主if/elseif/else控制塊之後將多個return語句移入一個return語句中。你也可以初始化$ message ['error'] = true;在主要的if塊之前,只有在它發生的一次時纔將其設置爲false。這應該使你的代碼更加簡潔/可讀,等等。 – codercake 2011-12-27 22:44:06