


When we register members on many websites, after the registration is completed, the system will automatically send an email to the user's mailbox. The content of this email is a URL link. The user needs to click and open this link to activate the registration on the website. account. Membership functions can be used normally only after activation is successful.
This article will use examples to explain 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.
Business process
1. The user submits registration information.
2. Write to the database. The account status 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.
Prepare the data table
The field Email 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 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
Place a registration form on the page, and the user can enter registration information, including user name, 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>
Necessary front-end verification is required for user input. Regarding the form verification function, it is recommended that you refer to the article on this site: An example explains the application of the form verification plug-in Validation. This article skips the front-end verification code. In addition, the page There should also be an input box that requires the user to re-enter the password, so you can skip it if you are lazy.
register.php
The user submits the registration information to register.php for processing. register.php needs to complete two major functions: writing data and sending emails.
First of all, it contains the two necessary files, connect.php and smtp.class.php. These two files are included in the download package provided outside. You are welcome to download.
include_once("connect.php");//连接数据库 include_once("smtp.class.php");//邮件发送类
Then we need to filter the information submitted by the user and verify whether the username exists (the front end can also verify).
$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 Composed and md5 encrypted. $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()){ $smtpserver = ""; //SMTP服务器,如:smtp.163.com $smtpserverport = 25; //SMTP服务器端口,一般为25 $smtpusermail = ""; //SMTP服务器的用户邮箱,如xxx@163.com $smtpuser = ""; //SMTP服务器的用户帐号xxx@163.com $smtppass = ""; //SMTP服务器的用户密码 $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //实例化邮件类 $emailtype = "HTML"; //信件类型,文本:text;网页:HTML $smtpemailto = $email; //接收邮件方,本例为注册用户的Email $smtpemailfrom = $smtpusermail; //发送邮件方,如xxx@163.com $emailsubject = "用户帐号激活";//邮件标题 //邮件主体内容 $emailbody = "亲爱的".$username.":<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/> <a href='http://www.helloweba.com/demo/register/active.php?verify=".$token."' target= '_blank'>http://www.helloweba.com/demo/register/active.php?verify=".$token."</a><br/> 如果以上链接无法点击,请将它复制到你的浏览器地址栏中进入访问,该链接24小时内有效。"; //发送邮件 $rs = $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); if($rs==1){ $msg = '恭喜您,注册成功!<br/>请登录到您的邮箱及时激活您的帐号!'; }else{ $msg = $rs; } } echo $msg;
There is also a very easy-to-use and powerful email sending class to share with everyone: use PHPMailer to send emails with attachments and support HTML content. You can use it directly.
active.php
If nothing goes wrong, the email you filled in when registering your account will receive an email from helloweba. At this time, you can directly click the activation link and hand it over to active.php deal with.
active.php接收提交的链接信息,获取参数verify的值,即激活识别码。将它与数据表中的用户信息进行查询对比,如果有相应的数据集,判断是否过期,如果在有效期内则将对应的用户表中字段status设置1,即已激活,这样就完成了激活功能。
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;
激活成功后,发现token字段并没有用处了,您可以清空。接下来我们会讲解用户找回密码的功能,也要用到邮箱验证,敬请关注。
以上就是(进阶篇)PHP实现用户注册后邮箱验证,激活帐号的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
