如何使用codeigniter从php中的一个函数调用变量到另一个函数?

时间:2017-09-26 09:37:54

标签: php function variables codeigniter-3

我的控制器

public function forgot_password(){
        $email = $this->input->post('email_recover');
        $token = bin2hex(openssl_random_pseudo_bytes(32));
        $findemail = $this->user_model->forgot_password($email);
        if ($findemail) {

                $config['protocol'] = "smtp";
                $config['smtp_host'] = "ssl://smtp.gmail.com";
                $config['smtp_port'] = "465";
                $config['smtp_user'] = "myemail@email.com"; 
                $config['smtp_pass'] = "password";
                $config['charset'] = "utf-8";
                $config['mailtype'] = "html";
                $config['newline'] = "\r\n";

                $this->email->initialize($config);

                $this->email->from('no-reply@admin.com', 'Admin');
                $this->email->to(set_value('email_recover'));

                $this->email->subject('Reset password');

                $email = $this->input->post('email');

                $this->email->message('<h3>Dear user.</h3><br><br><p>We want to help you to reset your password.</p>. <p>Here is the reset link &nbsp;'. base_url() .'login/reset_password/'.$token.'</p>');

                if(!$this->email->send()){
                    $this->session->set_flashdata('error', 'There is some error with your email. Please try again');

                }
                else{
                    $this->session->set_flashdata('success', 'Please check your email.');
                }
        } else {
                    redirect('login');
               }

    }

    public function reset_password(){
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('confirmpassword', 'Confirm Password', 'required');

        if($this->form_validation->run() === FALSE){
            $this->load->view('templates/pheader');
            $this->load->view('register/reset_password');
            $this->load->view('templates/pfooter');
        } else {
                    $this->user_model->reset_password();
                    //redirect('login/reset_password ');
               }

    }

我的模特

public function reset_password(){
        $data = array('password' => $this->input->post('password'));
        $this->db->where('email',$email);
        return $this->db->update('login_user', $data);
 }

请帮帮我。我想向用户发送恢复电子邮件链接,这是正常的。现在,当用户填写重置密码页面中的字段时,我无法确定该用户的电子邮件ID。任何人都可以告诉我如何访问以不同形式输入的电子邮件。

2 个答案:

答案 0 :(得分:0)

使用会话可以更新密码。请检查下面的代码我认为帮助你。

控制器:

public function forgot_password(){
        session_start();
        $email = $this->input->post('email_recover');
        $token = bin2hex(openssl_random_pseudo_bytes(32));
        $findemail = $this->user_model->forgot_password($email);
        if ($findemail) {

                $_Session['setEmail'] = array('email_recover' => $email,'token' => $token);

                $config['protocol'] = "smtp";
                $config['smtp_host'] = "ssl://smtp.gmail.com";
                $config['smtp_port'] = "465";
                $config['smtp_user'] = "myemail@email.com"; 
                $config['smtp_pass'] = "password";
                $config['charset'] = "utf-8";
                $config['mailtype'] = "html";
                $config['newline'] = "\r\n";

                $this->email->initialize($config);

                $this->email->from('no-reply@admin.com', 'Admin');
                $this->email->to(set_value('email_recover'));

                $this->email->subject('Reset password');

                $email = $this->input->post('email');

                $this->email->message('<h3>Dear user.</h3><br><br><p>We want to help you to reset your password.</p>. <p>Here is the reset link &nbsp;<a href="'. base_url() .'login/reset_password/'.$token.'">click</a></p>');

                if(!$this->email->send()){
                    $this->session->set_flashdata('error', 'There is some error with your email. Please try again');

                }
                else{
                    $this->session->set_flashdata('success', 'Please check your email.');
                }
        } else {
                    redirect('login');
               }

    }

    public function reset_password($token=NULL){

        if($_Session['setEmail']['token'] == $token){
            $email = $_Session['setEmail']['email_recover'];
        }

        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('confirmpassword', 'Confirm Password', 'required');

        if($this->form_validation->run() === FALSE){
            $this->load->view('templates/pheader');
            $this->load->view('register/reset_password');
            $this->load->view('templates/pfooter');
        } else {
                    $this->user_model->reset_password($email);
                    //redirect('login/reset_password ');
               }

    }

模型

public function reset_password($email){
        $data = array('password' => $this->input->post('password'));
        $this->db->where('email',$email);
        return $this->db->update('login_user', $data);
        $_Session['setEmail'] = '';
 }

此令牌传递链接和控制器中的检查后令牌正在使用url。更新密码重置会话变量后。

答案 1 :(得分:0)

您可以使用会话来存储电子邮件ID。 在控制器中的forgot_password()函数中,设置会话变量以存储电子邮件ID

<?php echo $_POST['id'];?>

您可以随意使用此变量。修改模型中的reset_password()函数:

$email = $this->input->post('email_recover');
$this->session->set_userdata('email', $email);