我需要一个非常简单的代码来更改已登录用户的密码。我使用以下代码: 控制器 - my_account.php
public function change_password(){
$this->page_handler->consumer_page();
$this->form_validation->set_rules('opassword','Old Password','required|trim|xss_clean|callback_change');
$this->form_validation->set_rules('npassword','New Password','required|min_length[6]|max_length[32]');
$this->form_validation->set_rules('cpassword','Confirm Password','required|matches[npassword]');
if($this->form_validation->run()!=true) {
$this->load->view("passwordchange");
}
else {
$sql = $this->db->select("*")->from("consumer")->where("login", $this->session->userdata("login"))->get();
foreach ($sql->result()as $info) {
$db_password = $info ->password;
$login = $info ->login;
}
if(md5($this->input->post("opassword"))==$db_password) {
$fixed_pw = md5(mysql_real_escape_string($this->input->post("npassword")));
$update = $this->db->query("UPDATE 'consumer' SET 'password' = '$fixed_pw' where 'login' ='$login'") or die(mysql_error());
$this->session->set_flashdata("notification","Password has been updated!");
redirect("MyPage","refresh");
}
else {
echo "Password is incorrect!";
$this->load->view("passwordchange");
}
}
}
模型 - page_handler.php
Class Page_handler extends CI_Model{
public function isLoggedIn(){
return $this->session->userdata("Logged_in");
}
public function consumer_page(){
if($this->isLoggedIn()) {
return TRUE;
}
}
}
我收到错误消息......
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: db_password
Filename: controllers/my_account.php
Line Number: 23
密码不正确! 哪里可以错?
答案 0 :(得分:0)
最好在控制器中定义两个方法。
第一:
function check_old_passwd($pass){
//for set custom validation message
$this->form_validation->set_message('check_old_passwd', 'your custom message');
//{check in db...}
if(true){
return true;
}
return false
}
第二:
function is_newpasswd_correct($newpass){
//for set custom validation message
$this->form_validation->set_message('is_newpasswd_correct', 'your custom message');
//check if new password is in your site terms
//not smaller than 4 digits etc.
if(true){
return true;
}
return false;
}
然后在验证规则中使用回调:
$this->form_validation->set_rules('oldpass', 'Old Password', 'required|trim|callback_check_old_passwd');
$this->form_validation->set_rules('newpass', 'New Password', 'required|trim|callback_is_newpasswd_correct');
希望它会帮助你