When we register members on many websites, the system will automatically send an email to the user's mailbox after registration. the content of this email is a URL link, you need to click this link to activate the account registered on the website. After successful activation, the member function can be used normally. When we register members on many websites, the system will automatically send an email to the user's mailbox after registration. the content of this email is a URL link, you need to click this link to activate the account registered on the website. After successful activation, the member function can be used normally.
This article describes how to use PHP + Mysql to register an account, send an activation email, verify the activation account, and process URL link expiration.
Business process
1. the user submits registration information.
2. write data to the database. at this time, the account status is not activated.
3. encrypt the user name and password or other identity characters to construct an activation code (you can also call it an activation code ).
4. send a URL consisting of a constructed activation identifier to the email address submitted by the user.
5. log on to your mailbox and click the URL to activate it.
6. verify the activation identifier. if the identifier is correct, activate the account.
Prepare a data table
The field Email in the user information table is very important. it can be used to verify the user, retrieve the password, or even collect the user information for Email marketing for the website. 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 'username ', 'password' varchar (32) not null comment 'password', 'Email 'varchar (30) not null comment 'mailbox', 'token' varchar (50) not null comment 'account activation code', 'token _ exptime' int (10) not null comment 'activation code', 'status' tinyint (1) not null default '0' comment' status, 0-NOT activated, 1-activated, 'regtime' int (10) not null comment' registration time ', primary key ('id') ENGINE = MyISAM default charset = utf8;
HTML
Place a registration form on the page. you can enter registration information, including the user name, password, and email address.
You need to perform necessary front-end verification for user input. for the form verification function, we recommend that you refer to the article on this site: the example explains the application of the form verification plug-in Validation. This article skipped the front-end verification code, in addition, there should be an input box asking the user to repeat the password on the page. this is just a bit lazy.
Register. php
The user submits the registration information to register. php for processing. Register. php requires writing data and sending emails.
First, it contains two necessary files: connect. php and smtp. class. php. these two files are available in the download package provided outside. welcome to download them.
Include_once ("connect. php"); // connect to the database include_once ("smtp. class. php"); // mail sending class
Then we need to filter the information submitted by the user and verify whether the user name 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 'user name already exists; change to another user name'; exit ;}
Then, we encrypt the user password and construct the activation identifier:
$ Password = md5 (trim ($ _ POST ['password']); // encrypted password $ email = trim ($ _ POST ['email ']); // email $ regtime = time (); $ token = md5 ($ username. $ password. $ regtime); // Create an identifier for activating $ token_exptime = time () + 60*60*24; // the expiration time is 24 hours later. $ 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 identifier, which consists of the 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. you can activate the account within this period. In this example, activation is effective within 24 hours. Finally, insert these fields into the t_user table.
After the data is inserted successfully, call the Mail sending class to send the activation information to the user's registered email address. Note that the constructed activation identifier is used as a complete URL as the activation link when the user clicks, the following code details:
If (mysql_insert_id () {$ smtpserver = ""; // SMTP server, for example, smtp.163.com $ smtpserverport = 25; // SMTP server port, generally 25 $ smtpusermail = ""; // SMTP server user mailbox, such as xxx@163.com $ smtpuser = ""; // SMTP server user account xxx@163.com $ smtppass = ""; // SMTP server user password $ smtp = new Smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // instantiate the mail class $ emailtype = "HTML "; // mail type, text: text; webpage: HTML $ smtpemailto = $ email; // recipient. In this example, the registered user's Email $ smtpemailfrom = $ smtpusermail is used; // send the email, such as xxx@163.com $ emailsubject = "User Account activation"; // mail Title // mail body content $ emailbody = "dear ". $ username. ":
Thank you for registering a new account on our site.
Click the link to activate your account.
Http://www.helloweba.com/demo/register/active.php? Verify = ". $ token ."
If the above link cannot be clicked, copy it to your browser's address bar for access. This link is valid for 24 hours. "; // Send an email $ rs = $ smtp-> sendmail ($ smtpemailto, $ smtpemailfrom, $ emailsubject, $ emailbody, $ emailtype); if ($ rs = 1) {$ msg = 'congratulations, registration successful!
Please log on to your mailbox and promptly activate your account! ';} Else {$ msg = $ rs;} echo $ msg;
Another useful and powerful email sending class is to share with you: you can use PHPMailer to send emails with attachments and HTML content.
Active. php
If nothing happens, the Email you entered during account registration will receive an Email sent by helloweba. at this time, you click the activation link and submit it to active. php for processing.
Active. php receives the submitted link information and obtains the verify parameter value, that is, the activation identifier. Compare it with the user information in the data table. if there is a corresponding dataset, determine whether it has expired. if it is within the validity period, set the status field of the corresponding user table to 1, that is, it is activated, this completes the activation function.
Include_once ("connect. php "); // connect to the database $ 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']) {// 24 hour $ msg = 'your activation has expired, please log on to your account and resend the activation email. ';} else {mysql_query ("update t_user set status = 1 where id = ". $ row ['id']); if (Mysql_affected_rows ($ link )! = 1) die (0); $ msg = 'activated successfully! ';}} Else {$ msg = 'Error.';} echo $ msg;
After successful activation, the token field is useless. you can clear it. Next, we will explain the user password retrieval function and use email verification. please stay tuned.