search
HomeBackend DevelopmentPHP TutorialPHP implements the function of activating user registration verification email

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 It 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 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[&#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;
}

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);

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=&#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;
}

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. .

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;


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 the function of activating user registration verification email. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools