php method to implement email verification: first download phpMailer; then unzip it to the project root directory; then you need a mail server; finally configure the email-related information in "config.php" and create a file for sending The public method of mail will do.
Recommended: "PHP Video Tutorial"
1. Introduction
Requirements The user fills in their email when registering. After registration, they need to verify their email before they can log in.
2. Implement the
verification process. After successful registration, a verification code is generated for the user; the verification code is sent to the user's mailbox in the form of a connection; the user clicks the connection to send the verification code back to the website; The website verifies whether the verification code is generated for this user; the account needs to be verified within 24 hours, otherwise the account will become invalid.
1. Preparation
You need to add the phpMailer class to send emails. Download phpMailer and extract it to the project root directory.
Send emails using the smtp protocol, the port is 25; receive emails, use the pop3 protocol, the port number is 110. A mail server is also required. You can build it yourself or use a third-party one. Using a third-party mail server requires registering an account with the third party. Log in and enable the smtp protocol.
Configure email-related information in config.php.
'EMAIL' => array( 'host' => 'smtp.163.com', 'user' => 'youxiangzhanghao', 'password' => 'password', //此处的密码为user邮箱在开启smtp时设置的授权密码。 ),
2. Create a public method for sending emails
/** * 发送邮件的方法 */ functionsendMail($to, $title, $content) { require_once('./PHPMailer-5.2.14/class.smtp.php'); require_once('./PHPMailer-5.2.14/class.phpmailer.php'); $mail = new PHPMailer(); //设置为发送邮件 $mail->IsSMTP(); //是否允许发送html代码为邮件内容 $mail->IsHTML(true); $mail->CharSet = 'utf-8'; //是否需要身份验证 $mail->SMTPAuth = true; //邮件服务器的帐号信息 $mailConfig = C('EMAIL'); $mail->From =$mailConfig['user'].'@163.com'; $mail->FromName =$mailConfig['user']; $mail->Host = $mailConfig['host']; $mail->Username = $mailConfig['user']; $mail->Password =$mailConfig['password']; //发邮件的端口号 $mail->Port = 25; //收件人 $mail->AddAddress($to); //邮件标题 $mail->Subject = $title; //邮件内容 $mail->Body = $content; return($mail->send()); }
Test in the controller's homepage method
//测试发送邮件 var_dump(sendMail('haha@163.com','phptestemail', '<a href="http://www.phptest.com">hahahah</a>'));
3. Modify the member table, add email field and verification Code field
email varchar(150) not null comment '邮箱', email_chkcode char(32) not null default '' comment '邮箱验证码', email_chkcode_time int unsigned not null comment '邮箱验证码生成时间', key email_chkcode(email_chkcode)
4. Modify registration, email must be filled in
Modify the registration form, add the email field; modify the member model to allow the insertion of the email field, and add the verification rule for the email field as email.
5. Generate email verification code before registration
In the _before_insert() method
//生成email验证码 $data['email_chkcode']= md5(uniqid()); $data['email_chkcode_time']= time();
6. After registration, send the verification code to the user’s email address
/** * 注册后的钩子函数 */ protected function _after_insert($data,$option) { $content = "欢迎注册,请点击以下完成验证:<p><a href='http://www.php2.com/eshop/index.php/Home/Member/email_chk/code/{$data['email_chkcode']}'>请点击</a></p>"; sendMail($data['email'],'php2网站邮箱验证', $content); }
7. Add a method in the member controller to complete the verification
/** * 完成邮箱验证方法 */ public function email_chk() { //接收验证码 $code = I('get.code'); //查询这个验证码是否有效 $member = D('Admin/Member'); $info = $member->field('id,email_chkcode_time')->where(array( 'email_chkcode' => $code, ))->find(); if($info) { if((time() -$info['email_chkcode_time']) > 86400) { //帐号过期,删除这个帐号 $member->delete($info['id']); $this->error('验证码已过期,帐号已经删除,请重新注册', U('regist')); exit; } else { //验证通过,将验证码清空 $member->where(array( 'id' => $info['id'], ))->setFielf('email_chkcode', ''); $this->success('验证通过,请登陆', U('login')); exit; } } else { $this->error('参数错误', U('/')); exit; } }
8. adding a method to complete the verification
//判断这个帐号是否通过验证码验证 if(!empty($user['email_chkcode'])) { $this->error= '必须验证后才可以登陆'; returnfalse; }
8. out out out out out of ’s to be verified to be verified to have It exists, but it has expired and has not been verified. You can delete this account.
Add form verification during registration, and add verification on uniqueness and registration verification code for username and email.
array('email','chk_un_email', 'email already exists!', 1,'callback', 3),
The name already exists!', 1,'callback', 3),
//验证已经存在的邮箱是否通过验证 public function chk_un_email() { $email = I('post.email'); $username =I('post.username'); $hasEmail =$this->field('id,email_chkcode,email_chkcode_time')->where(array( 'email' =>$email, ))->find(); if($hasEmail) { if(empty($hasEmail['email_chkcode'])) { returnfalse; } else { if(time() -$hasEmail['email_chkcode_time'] > 86400) { $this->delete($hasEmail['id']); returntrue; } else { returnfalse; } } } else { return true; } } //验证已经存在的用户名是否通过验证 public function chk_un_user() { $username =I('post.username'); $hasUsername =$this->field('id,email_chkcode,email_chkcode_time')->where(array( 'username' =>$username, ))->find(); if($hasUsername) { if(empty($hasUsername['email_chkcode'])) { returnfalse; } else { if(time() -$hasUsername['email_chkcode_time'] > 86400) { $this->delete($hasUsername['id']); returntrue; } else { returnfalse; } } } else { return true; } }
10. Test
The above is the detailed content of PHP implements email verification. For more information, please follow other related articles on the PHP Chinese website!