要检索忘记的密码

时间:2013-11-12 06:05:11

标签: php codeigniter

在下面的代码我使用了codeigniter代码我输入用户名和密码到db。但我想要检索我忘记密码。我试过但我得到以下错误请帮助重新解决问题。

控制器:

<?php

class Login extends CI_Controller {

    function index()
    {
        $data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);      
    }

    function validate_credentials()
    {       
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('site1/members_area');
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }   

    function signup()
    {
        $data['main_content'] = 'signup_form';
        $this->load->view('includes/template', $data);
    }

    function create_member()
    {
        $this->load->library('form_validation');

        // field name, error message, validation rules
        $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
         $this->load->helper('date');

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('signup_form');
        }

        else
        {           
            $this->load->model('membership_model');

            if($query = $this->membership_model->create_member())
            {
                $data['main_content'] = 'signup_successful';
                $this->load->view('includes/template', $data);
            }
            else
            {
                $this->load->view('signup_form');           
            }
        }

    }
    public function forgot_password()
{
    $this->form_validation->set_rules('email_address','Email Address','trim|required|valid_email|callback__check_email_exists');

    if($this->form_validation->run() == FALSE)
    {
       // VALIDATION ERRORS, SHOW VIEWS
       $this->index();
    }
    else
    {
      // ALL IS GOOD, UPDATE EMAIL, AND REDIRECT TO CURRENT URL
      $this->load->view('login_form');
    }
}
public function _check_email_exists($email)
{
  // LOAD AND USE YOUR MODEL TO CHECK EMAIL EXISTS HERE
  if ( ! $email_exists )
  {
    $this->form_validation->set_message('email_address', 'That email address don\'t exist, sucka!');
    return FALSE;
  }
  else
  {
    return TRUE;
  }
}
    function logout()
    {
        $this->session->sess_destroy();
        $this->index();
    }

}

模型

<?php

class Membership_model extends CI_Model {

    function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('membership');

        if($query->num_rows == 1)
        {
            return true;
        }

    }

    function create_member()
    {

        $new_member_insert_data = array(
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email_address' => $this->input->post('email_address'),         
            'username' => $this->input->post('username'),
            'date' => date('Y-m-d H:i:s', now()),
            'password' => md5($this->input->post('password'))                       
        );

        $insert = $this->db->insert('membership', $new_member_insert_data);
        if ($this->db->_error_number() == 1062)
                {
                echo"<script>alert('This value already exists');</script>";
                }
                if ($this->db->_error_number() == "")
                {
                $this->session->set_flashdata('create', 'create');
                }

        return $insert;
    }


    public function val_forgot_password()
{
    $this->db->where('email_address', $this->input->post('email_address'));
    $query = $this->db->get($this->members);

    if($query->num_rows == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
    function curdate() {
    // gets current timestamp
    date_default_timezone_set('Asia/Manila'); // What timezone you want to be the default value for your current date.
    return date('Y-m-d H:i:s');
}
}

视图:login_form

<?php $this->load->view('includes/header'); ?>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style1.css"  />
<div id="login_form">

    <h1>Login!</h1>
    <?php 
    echo form_open('login/validate_credentials');
    echo form_input('username', 'Username');
    echo form_password('password', 'Password');
    echo form_submit('submit', 'Login');
    echo anchor('login/signup', 'Create Account');
    echo form_close();
    ?>

</div><!-- end login_form-->


<?php $this->load->view('includes/footer'); ?>
<?php

echo form_open('login/forgot_password');
echo "Email Address: " . form_input('email_address', set_value('email_address', ''));
echo br(2);
echo form_submit('submit','Send Email');
echo form_close();
echo br(1);

echo validation_errors('<p>Error: ');

?>

1 个答案:

答案 0 :(得分:0)

首先,您需要控制器中的一个功能才能将用户带到忘记的用户页面。

public function forgot_pass(){
 $this->load->view('forgot_pass_view');
}

然后,您可能需要至少询问视图中的电子邮件地址。 创建另一个函数来验证控制器中的电子邮件地址,如果匹配则发送带有新临时密码的邮件。

public function check_email(){
  $this->load->helper(array('form', 'url'));
  $this->load->library('form_validation');
  $this->form_validation->set_rules('email', 'email_check', 'callback_email_check');
  if ($this->form_validation->run() == FALSE){
   //throw error
  } else {
   //send email
  }
}
public function email_check(){
  //use this function to check if the email exists and return false if it is not the DB
}