PHP implements user registration and activation email function sample code

怪我咯
Release: 2023-03-10 14:28:01
Original
1803 people have browsed it

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;
Copy after login

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>
Copy after login

register.php completes writing data and sending emails

First connect to the database And contains the mail sending class smtp.class.php

include_once("connect.php");//连接数据库
include_once("smtp.class.php");//邮件发送类
Copy after login

We omit the front-end verification form and look directly at the program

$username = stripslashes(trim($_POST[&#39;username&#39;]));
$query = mysql_query("select id from t_user where username=&#39;$username&#39;");
$num = mysql_num_rows($query);
if($num==1){
  echo &#39;用户名已存在,请换个其他的用户名&#39;;
  exit;
}
Copy after login

Then we encrypt the user password and construct the activation identification code:

$password = md5(trim($_POST[&#39;password&#39;])); //加密密码
$email = trim($_POST[&#39;email&#39;]); //邮箱
$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 (&#39;$username&#39;,&#39;$password&#39;,&#39;$email&#39;,&#39;$token&#39;,&#39;$token_exptime&#39;,&#39;$regtime&#39;)";
mysql_query($sql);
Copy after login

In the above code, $token is the constructed activation code 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=&#39;http://www.jb51.net/demo/active.php?verify=" . $token . "&#39; target=&#39;_blank&#39;>http://www.jb51.net/demo/active.php?verify=" . $token . "</a><br/>如果以上链接无法点击,请将它复制到你的浏览器地址栏中进入访问,该链接24小时内有效。<br/>如果此次激活请求非你本人所发,请忽略本邮件。<br/><p style=&#39;text-align:right&#39;>-------- 脚本之家http://www.jb51.net敬上</p>";
  $rs = $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype);
  if ($rs == 1) {
    $msg = &#39;恭喜您,注册成功!<br/>请登录到您的邮箱及时激活您的帐号!&#39;;
  } else {
    $msg = $rs;
  }
  echo $msg;
}
Copy after login

active.php

active.php receives submitted link information , get the value of 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. .

include_once("connect.php");//连接数据库
$verify = stripslashes(trim($_GET[&#39;verify&#39;]));
$nowtime = time();
$query = mysql_query("select id,token_exptime from t_user where status=&#39;0&#39; and
`token`=&#39;$verify&#39;");
$row = mysql_fetch_array($query);
if($row){
  if($nowtime>$row[&#39;token_exptime&#39;]){ //24hour
    $msg = &#39;您的激活有效期已过,请登录您的帐号重新发送激活邮件.&#39;;
  }else{
    mysql_query("update t_user set status=1 where id=".$row[&#39;id&#39;]);
    if(mysql_affected_rows($link)!=1) die(0);
    $msg = &#39;激活成功!&#39;;
  }
}else{
  $msg = &#39;error.&#39;;
}
echo $msg;
Copy after login

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.

The above is the detailed content of PHP implements user registration and activation email function sample code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template