This article mainly introduces the function of activating user registration and verification mailbox implemented in PHP, and analyzes in detail the database and email related operation skills involved in activating users through PHP mail. Friends in need can refer to the example of this article
Describes the function of activating user registration and verification email implemented in PHP. Share it with everyone for your reference, the details are as follows:
Here will be combined with examples to introduce how to use PHP+Mysql to complete the functions of registering an account, sending activation emails, verifying the activation account, and handling URL link expiration.
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.
t_user.sql
The field Email in the user information table is very important. It can be used to verify users, retrieve passwords, and even modify the website Fang Lai can be used to collect user information for email marketing. The following is the table structure of the user information table 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
The following is a registration form where users can enter registration information, including username, password and email.
<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 include emails Send class smtp.class.php
##
include_once("connect.php");//连接数据库 include_once("smtp.class.php");//邮件发送类
$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; }
$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);
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='http://www.jb51.net/demo/active.php?verify=" . $token . "' target='_blank'>http://www.jb51.net/demo/active.php?verify=" . $token . "</a><br/>如果以上链接无法点击,请将它复制到你的浏览器地址栏中进入访问,该链接24小时内有效。<br/>如果此次激活请求非你本人所发,请忽略本邮件。<br/><p style='text-align:right'>-------- 脚本之家http://www.jb51.net敬上</p>"; $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 parameters The value of verify 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. .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;
The above is the detailed content of PHP implements verification email to activate new user login function after registration. For more information, please follow other related articles on the PHP Chinese website!