我有两个文件
SiteController.php
public function actionPassword(){
$pass = (isset($_POST['pass']) && $_POST['pass'] != "" ? $_POST['pass'] : "");
$newPass = (isset($_POST['newpass']) && $_POST['newpass'] != '' ? $_POST['newpass'] : '');
$confirmPass = (isset($_POST['confirmpass']) && $_POST['confirmpass'] != '' ? $_POST['confirmpass'] : '');
$result = Yii::app()->params['result'];
if($pass == '' || $newPass == '' || $confirmPass == '' ){
$result['msg'] = "Empty password!";
}else{
$dbUser = new Users;
$findUser = $dbUser->find('username = :user', array(':user' => $user));
$verify = CPasswordHelper::verifyPassword($pass, $findUser->password);
if(!$verify){
$result['msg'] = "Wrong password!";
}else{
if($verify->status_id == 0 || $verify->status_id == '-1'){
$result['msg'] = "This password does not exist";
}else if($verify->status_id == 1){
if ($newPass == $confirmPass){
if($dbUser->save()){
$result['code'] = 1000;
$result['error'] = 1;
$result['msg'] = 'Password successfuly changed';
}else{
$result['code'] = 1005;
$result['error'] = 0;
$result['msg'] = $dbUser->error;
}
}
}
}
}
$json = json_encode($result);
echo $json;
}
public function actionViewPassword() {
$this->checkLogin();
$this->render('password');
}
和另一个
的文件password.php
<div class="view_container">
<div class="pagetitle">Change Password</div>
<div class="label_row">
<div class="label">Old Password: </div>
<div class="input_label"><input type="text" value="" placeholder="Old Password" id="pass" /></div>
</div>
<div class="label_row">
<div class="label">New Password: </div>
<div class="input_label"><input type="text" id="newpass" value="" placeholder="New Password" /></div>
</div>
<div class="label_row">
<div class="label">Confirm New Password: </div>
<div class="input_label"><input type="text" id="confirmpass" value="" placeholder="Confirm New Password" /></div>
</div>
<div class="label_row">
<div class="label"></div>
<div class="input_label"><input type="submit" id="btnSubmit" value="Confirm" /></div>
</div>
</div>
<script>
$(document).ready(function(){
var userurl = '<?php echo Yii::app()->createAbsoluteUrl('user/password'); ?>';
$('#btnSubmit').click(function(){
var JoldPass = $('#pass').val();
var JnewPass = $('#newpass').val();
var JconfirmPass = $('#confirmpass').val();
loading(true);
$.ajax({
type: "POST",
url: userurl,
data: {
pass: JoldPass,
newpass: JnewPass,
confirmpass: JconfirmPass
},
dataType: "json",
}).done(function(e) {
alert(e.msg);
}).fail(function(e){
alert(e.error);
});
});
});
</script>
好的,这是一个问题,我想让网页在这里用新密码替换旧密码。我已经尝试了几天,但我无法得到解决方案。我希望数据库成功更改旧密码。我在这里缺少什么?
输入旧密码:11111
输入新密码:22222
确认新密码:22222
现在新密码是22222.我很高兴有人可以帮助我,因为我是PHP的新手。
答案 0 :(得分:0)
我重写了一些你的代码,
public function actionPassword(){
if(empty($_POST['pass']) || empty($_POST['newpass']) || empty($_POST['confirmpass']) ){
$result['msg'] = "Empty password!";
$this->returnResult($result);
return;
}
$pass = $_POST['pass'];
$newPass = $_POST['newpass'];
$confirmPass = $_POST['confirmpass'];
$result = Yii::app()->params['result'];
$dbUser = Users::model()->find('username = :user', array(':user' => $user));
if(empty($dbUser)){
$result['msg'] = "User not found!";
$this->returnResult($result);
return;
}
$verify = CPasswordHelper::verifyPassword($pass, $dbUser->password);
if(!$verify){
$result['msg'] = "Wrong password!";
$this->returnResult($result);
return;
}
//This means nothing, verifyPassword return true or false, not an object
if($verify->status_id == 0 || $verify->status_id == '-1'){
$result['msg'] = "This password does not exist";
$this->returnResult($result);
return;
}
if($verify->status_id == 1 && $newPass == $confirmPass){
if($dbUser->save()){
$result['code'] = 1000;
$result['error'] = 1;
$result['msg'] = 'Password successfuly changed';
$this->returnResult($result);
return;
}
$result['code'] = 1005;
$result['error'] = 0;
$result['msg'] = $dbUser->error;
$this->returnResult($result);
}
}
protected function returnResult($result)
{
$json = json_encode($result);
echo $json;
}
现在有几个问题:
$verify->status_id
,但$verify
是verifyPassword
的结果,它返回true
或false
,但不是对象!$dbUser