我已经为我的一个项目设置了密码重置功能。
我不确定,如何将代码链接在一起。
我已经准备好使用电子邮件功能,重置功能和重置链接。但是我不确定如何从视图和电子邮件中获取重置链接以邮寄到输入的电子邮件地址,以包含我的重置令牌和消息。
请查看我的代码,并在可能的情况下提出建议,非常感谢。
这是我的用户控制器代码
public function Email($data){
$config = array(
'useragent' => 'codeIgniter',
'protocol' => 'mail',
'mailpath' => '/usr/sbin/sendmail',
'smtp_host' => 'localhost',
'smtp_user' => 'Myemail',
'smtp_pass' => 'mypass',
'smtp_port' => 25,
'smtp_timeout' => 55,
'wordwrap' => TRUE,
'wrapchars' => 76,
'mailtype' => 'html',
'charset' => 'utf-8',
'validate' => FALSE,
'priority' => 3,
'crlf' => "\r\n",
'newline' => "\r\n",
'bcc_batch_mode' => FALSE,
'bcc_batch_size' => 200,
);
$this->load->library('email', $config);
$this->email->set_newline('\r\n');
$this->email->from('myemail');
$this->email->to(''); //This is where I'm not sure.
$this->email->subject("Reset Password");
$this->email->message(); //Next point where I'm not sure
$this->email->set_mailtype('html');
if($this->email->send()){
return TRUE;
}
else{
return FALSE;
}
}
public function forgotpassword(){
$this->load->view('templates/header');
$this->load->view('users/forgotpassword');
$this->load->view('templates/footer');
}
public function resetlink(){
$email = $this->input->post('email');
$result = $this->db->query("select * from users where
email ='".$email."'")->result_array();
if(count($result)>0){
$token = rand(100000,999999);
$this->db->query("update users set password =
'".$token."' where email = '".$email."'");
$message = "Please click on the password reset link
<br><a href='".base_url('users/reset?token=').$token."'>Reset
Password</a>";
$this->Email($email, 'Reset Password Link', $message);
}
else{
$this->session->set_flashdata('message', 'Email not
registered');
redirect(base_url('users/forgotpassword'));
}
}
public function resetpass(){
$data['token'] = $this->input->get('token');
$_SESSION['token'] = $data['token'];
$this->load->view('templates/header');
$this->load->view('users/resetpass');
$this->load->view('templates/footer');
}
public function updatepass(){
$_SESSION['token'];
$data = $this->input->post();
if ($data['password'] == $data ['cpassword']){
$this->db->query("update users set
password'".$data['password']."' where password='".$_SESSION['token']."'");
}
}
这是我忘记密码的视图
<style>
body{
background-image:
url("/assets/images/posts/background12.jpg");
background-repeat: no-repeat;
background-size: cover;
}
</style>
<br>
<br>
<form action="<?= base_url('users/resetlink')?>"
method="post">
<div class="row" >
<div class="col-md-3"></div>
<div class="col-md-6">
<div class="form-group" >
<h2 align="center" >Reset your Password</h2>
<br>
<input type="email" class="form-control"
name="email" placeholder="Email" required autofocus>
</div>
<input style="margin-top: 15px" type="submit"
class="btn btn-info btn-block" value="Send reset link">
<h3 style="margin-top: 30px" align="center"><?= $this-
>session->flashdata('message') ?></h3>
</div>
<div class="col-md-3"></div>
</div>
</form>
重置密码视图
<style>
body{
background-image: url("/assets/images/posts/background12.jpg");
background-repeat: no-repeat;
background-size: cover;
}
</style>
<br>
<br>
<form action="<?= base_url('users/resetpass') ?>">
<div class="row" >
<div class="col-md-3"></div>
<div class="col-md-6">
<h1>Reset Password</h1>
<div class="form-group" >
<h1 align="center"><?= $this->session->flashdata('message') ?></h1>
<input type="password" id="email" class="form-control"
name="password" placeholder="Password" required autofocus>
</div>
<div class="form-group" >
<input type="password" id="email" class="form-control"
name="cpassword" placeholder="Confirm Password" required autofocus>
</div>
<input style="margin-top: 15px" type="submit" class="btn btn-info
btn-block" value="Reset password">
</div>
<div class="col-md-3"></div>
</div>
</form>
这就是我为此得到的所有相关代码。任何帮助将不胜感激
答案 0 :(得分:1)
如果您将电子邮件功能更改为
public function Email($email, $subject, $content){
$config = array(
'useragent' => 'codeIgniter',
'protocol' => 'mail',
'mailpath' => '/usr/sbin/sendmail',
'smtp_host' => 'localhost',
'smtp_user' => 'Myemail',
'smtp_pass' => 'mypass',
'smtp_port' => 25,
'smtp_timeout' => 55,
'wordwrap' => TRUE,
'wrapchars' => 76,
'mailtype' => 'html',
'charset' => 'utf-8',
'validate' => FALSE,
'priority' => 3,
'crlf' => "\r\n",
'newline' => "\r\n",
'bcc_batch_mode' => FALSE,
'bcc_batch_size' => 200,
);
$this->load->library('email', $config);
$this->email->set_newline('\r\n');
$this->email->from('myemail');
$this->email->to($email); //email address passed into the function.
$this->email->subject($subject); // subject passed into the function
$this->email->message($content); //content passed into the function
$this->email->set_mailtype('html');
if($this->email->send()){
return TRUE;
}
else{
return FALSE;
}
}
但是,根据[Codeigniter文档] [1]
[1]:https://www.codeigniter.com/user_guide/libraries/email.html?highlight=complete%20web,您需要发送完整的网页。因此,您将需要-
$message = "<html><head><title>Password Reset</title></head><body>Please click on the password reset link <br><a href='".base_url('users/reset?token=').$token."'>Reset Password</a></body></html>";
$this->Email($email, 'Reset Password Link', $message);