Home > Article > Backend Development > Detailed explanation of how to implement user email registration activation code in PHP
When users log in and register on the website, they usually need to use the user's own email address. The email address receives the email from the website to activate the account, and then it can be used normally. This article will use examples to introduce how to use PHP+Mysql to complete the functions of registering an account, sending activation emails, verifying activation accounts, and handling URL link expiration. Receive an email with a link to activate.
Registration email activation process
#1. User registration
2. Insert user data, the account is not activated at this time .
3. Encrypt the username, password or other identifying characters to form an activation identification code (you can also call it an activation code).
4. Send the constructed activation identification code into a URL to the email address submitted by the user.
5. The user logs in to the email and clicks on the URL to activate.
6. Verify the activation identification code. If correct, activate the account.
Create databaseTable
When we register an account, it is usually recorded in the user table Our ID, username, password, email or mobile phone number, and a field indicating whether the account is activated or not.
The email field in the user information table is very important. It can be used to verify users, retrieve passwords, and even for website parties, it can be used to collect user information for email marketing. The following is the user information table Table structure of t_user:
CREATE TABLE IF NOT EXISTS `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL COMMENT '用户名', `password` varchar(32) NOT NULL COMMENT '密码', `email` varchar(30) NOT NULL COMMENT '邮箱', `token` varchar(50) NOT NULL COMMENT '帐号激活码', `token_exptime` int(10) NOT NULL COMMENT '激活码有效期', `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态,0-未激活,1-已激活', `regtime` int(10) NOT NULL COMMENT '注册时间', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
HTML static page
The following is a registration form. Users can enter registration information, including user name and password. and mailbox.
<form id="reg" action="register.php" method="post"> <p>用户名:<input type="text" class="input" name="username" id="user"></p> <p>密 码:<input type="password" class="input" name="password" id="pass"></p> <p>E-mail:<input type="text" class="input" name="email" id="email"></p> <p><input type="submit" class="btn" value="提交注册"></p> </form>
register.php completes writing data and sending emails
First connect to the database and contains the email sending class smtp .class.php
include_once("connect.php");//连接数据库 include_once("smtp.class.php");//邮件发送类
We omit the front-end verification form and look directly at the program
$username = stripslashes(trim($_POST['username'])); $query = mysql_query("select id from t_user where username='$username'"); $num = mysql_num_rows($query); if($num==1){ echo '用户名已存在,请换个其他的用户名'; exit; }
Then we encrypt the user password and construct the activation identification code:
$password = md5(trim($_POST['password'])); //加密密码 $email = trim($_POST['email']); //邮箱 $regtime = time(); $token = md5($username.$password.$regtime); //创建用于激活识别码 $token_exptime = time()+60*60*24;//过期时间为24小时后 $sql = "insert into `t_user` (`username`,`password`,`email`,`token`,`token_exptime`,`regtime`) values ('$username','$password','$email','$token','$token_exptime','$regtime')"; mysql_query($sql);
In the above code , $token is the constructed activation identification code, which is composed of user name, password and current time and is encrypted by md5. $token_exptime is used to set the expiration time of the activation link URL. The user can activate the account within this time period. In this example, the activation is valid within 24 hours. Finally, these fields are inserted into the data table t_user.
When the data is inserted successfully, call the email sending class to send the activation information to the user's registered mailbox. Pay attention to forming the constructed activation identification code into a complete URL as the activation link when the user clicks. The following is the details Code:
if (mysql_insert_id()) {//写入成功,发邮件 include_once("smtp.class.php"); $smtpserver = "smtp.163.com"; //SMTP服务器 $smtpserverport = 25; //SMTP服务器端口 $smtpusermail = "hjl416148489_4@163.com"; //SMTP服务器的用户邮箱 $smtpuser = "hjl416148489_4@163.com"; //SMTP服务器的用户帐号 $smtppass = "hjl7233163"; //SMTP服务器的用户密码 $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //这里面的一个true是表示使用身份验证,否则不使用身份验证. $emailtype = "HTML"; //信件类型,文本:text;网页:HTML $smtpemailto = $email; $smtpemailfrom = $smtpusermail; $emailsubject = "用户帐号激活"; $emailbody = "亲爱的" . $username . ":<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/> <a href='//m.sbmmt.com/demo/active.php?verify=" . $token . "' target='_blank'>//m.sbmmt.com/demo/active.php?verify=" . $token . "</a> <br/>如果以上链接无法点击,请将它复制到你的浏览器地址栏中进入访问,该链接24小时内有效。<br/>如果此次激活请求非你本人所发,请忽略本邮件。<br/> "; $rs = $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); if ($rs == 1) { $msg = '恭喜您,注册成功!<br/>请登录到您的邮箱及时激活您的帐号!'; } else { $msg = $rs; } echo $msg; }
active.php
active.php receives the submitted link information and obtains the value of the parameter verify, which is the activation identification code. Query and compare it with the user information in the data table. If there is a corresponding data set, determine whether it has expired. If it is within the validity period, set the status field in the corresponding user table to 1, which means it has been activated. This completes the activation function. .
$email is the email we used when registering. We use urlencode() to encode it and use the string for the request part of the URL. $activation_key is the activation code we generated. We use the server to send this email Send an activation email, and this will be displayed as a hyperlink in the email client, prompting you to click. After clicking, we will transfer the email and key to the activate.php file, which means we will start to perform verification and activate the account.
include_once("connect.php");//连接数据库 $verify = stripslashes(trim($_GET['verify'])); $nowtime = time(); $query = mysql_query("select id,token_exptime from t_user where status='0' and `token`='$verify'"); $row = mysql_fetch_array($query); if($row){ if($nowtime>$row['token_exptime']){ //24hour $msg = '您的激活有效期已过,请登录您的帐号重新发送激活邮件.'; }else{ mysql_query("update t_user set status=1 where id=".$row['id']); if(mysql_affected_rows($link)!=1) die(0); $msg = '激活成功!'; } }else{ $msg = 'error.'; } echo $msg;
After successful activation, you find that the token field is no longer useful and you can clear it. And the status activation status changes to 1. Now the account is activated. When the user logs in, first check whether the activation is activated, and then proceed to the subsequent steps.
The above is the detailed content of Detailed explanation of how to implement user email registration activation code in PHP. For more information, please follow other related articles on the PHP Chinese website!