【问题标题】:Error if email is exists - codeigniter php如果电子邮件存在则出错 - codeigniter php
【发布时间】:2016-11-15 21:36:12
【问题描述】:

当我运行忘记页面时,我遇到了以下问题,电子邮件检查是否工作正常,但如果电子邮件存在,则邮件发送部分会出现以下错误,而不是电子邮件发送。

User.php(控制器)

public function forgot() {

// create the data object
$data = new stdClass();

// load form helper and validation library
$this->load->helper('form');
$this->load->library('form_validation');

$this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 

if($this->form_validation->run() == FALSE) {
    // Load this page with error
    $this->load->view('header');
    $this->load->view('user/forgot/forgot');
    $this->load->view('footer');
}else{
    $email = trim($this->input->post('email'));  
    //$clean = $this->security->xss_clean($email);
    $userInfo = $this->user_model->checkUserEmail($email);

    if($userInfo) {
        $this->password_to_email($email, $userInfo);
        $this->load->view('header');
        $this->load->view('user/forgot/password_sent', array('email' => $email));
        $this->load->view('footer');
    } else {
        $data->error = 'We cant find your email address.';

        $this->load->view('header');
        $this->load->view('user/forgot/forgot', $data);
        $this->load->view('footer');
    }

}

}

私有函数 password_to_email($email, $firstname) { $this->load->library('email');

$from_email = "test@test.com";

$token = md5($this->config->item('salt') . $firstname);

$this->email->set_mailtype('html');
$this->email->from($from_email);
$this->email->to($email);
$this->email->message('Reset your Password');

$message = '<html><body>';
$message .= '<p>Hello '.$firstname.'</p>';
$message .= '<p>Please use following link to reset your password.<br><a href="'.base_url().'reset/reset/'.$email.'/'.$token.'"></a></p>';
$message .= '<p>Thank you!</p>';
$message .= '</body></html>';

$this->email->message($message);
$this->email->send();

}

【问题讨论】:

    标签: php codeigniter email


    【解决方案1】:

    好吧,您还没有展示 $this->password_to_email($email, $userInfo); 的模型,所以我只需要在这里猜测一下。

    线索就在错误中

    消息:stdClass 类的对象无法转换为字符串。

    但是将会发生的是 $firstname in

    $token = md5($this->config->item('salt') . $firstname);
    

    需要是一个字符串,并且您从数据库查询中传入一个对象。

    所以你打电话给

    $this->password_to_email($email, $userInfo);
    

    应该是这样的

    $this->password_to_email($email, $userInfo->firstname);
    

    所以你需要确保你传入的是一个字符串。

    执行 var_dump($userInfo);in say...

    ...
    }else{
        $email = trim($this->input->post('email'));  
        //$clean = $this->security->xss_clean($email);
        $userInfo = $this->user_model->checkUserEmail($email)
        var_dump($userInfo);
    ...
    

    将向您展示 $userInfo 的实际格式。然后你可以从那里去。你只需要确保你得到 $firstname 作为一个字符串。

    【讨论】:

    • 通过 vardumb 我正在关注 object(stdClass)#23 (12) { ["id"]=&gt; string(1) "4" ["firstname"]=&gt; string(8) "Rahamath" ["lastname"]=&gt; string(2) "MK" ["username"]=&gt; string(9) "krahamath" ["email"]=&gt; string(26) "myname@gmail.com" ["password"]=&gt; string(60) "$2y$10$EhhXwbTLCSVpK4kE42f1neYFNkMn6GlWkTMjGY/ZL1ySbi5xdruiG" ["avatar"]=&gt; string(11) "default.jpg" ["created_at"]=&gt; string(19) "2016-11-13 22:24:58" ["updated_at"]=&gt; NULL ["is_admin"]=&gt; string(1) "0" ["is_confirmed"]=&gt; string(1) "0" ["is_deleted"]=&gt; string(1) "0" }
    • 更改后工作正常。谢谢 $this->password_to_email($email, $userInfo->firstname);
    • 太棒了 :) 不客气!我只是在这里检查我的测试代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    相关资源
    最近更新 更多