Example of activating the email address for user registration and verification in PHP

Source: Internet
Author: User

Example of activating the email address for user registration and verification in PHP

The example in this article describes how to activate the user registration and verification email function implemented by PHP. We will share this with you for your reference. The details are as follows:

This section describes how to use PHP + Mysql to register an account, send an activation email, verify the activation account, and process URL link expiration.

Email Activation Process

1. User Registration
2. Insert user data. At this time, the account 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.

T_user. SQL

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;


The following is a registration form. You can enter registration information, including the user name, password, and email address.

<Form id = "reg" action = "register. php "method =" post "> <p> User Name: <input type = "text" class = "input" name = "username" id = "user"> </p> <p> password: <input type = "password" class = "input" name = "password" id = "pass"> </p> <p> E-mail: <input type = "text" class = "input" name = "email" id = "email"> </p> <input type = "submit" class =" btn "value =" Submit Registration "> </p> </form>

Register. php writes data and sends emails

First, connect to the database and include the mail sending class smtp. class. php

Include_once ("connect. php"); // connect to the database include_once ("smtp. class. php"); // mail sending class

The front-end verification form is omitted.

$ 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 () {// The write is successful. Send the email include_once ("smtp. class. php "); $ smtpserver =" smtp.163.com "; // SMTP server $ smtpserverport = 25; // SMTP server port $ smtpusermail =" hjl416148489_4@163.com "; // SMTP server user email $ smtpuser = "hjl416148489_4@163.com"; // SMTP server user account $ smtppass = "hjl7232.163 "; // SMTP server user password $ smtp = new Smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // here, true indicates that authentication is used, otherwise, authentication is not used. $ Emailtype = "HTML"; // mail type, text: text; webpage: HTML $ smtpemailto = $ email; $ smtpemailfrom = $ smtpusermail; $ emailsubject = "User Account Activation "; $ emailbody = "dear ". $ username. ": <br/> Thank you for registering a new account on our site. <Br/> click the link to activate your account. <Br/> <a href = 'HTTP: // www.bkjia.com/demo/active.php? Verify = ". $ token." 'target = '_ blank'> http://www.bkjia.com/demo/active.php? Verify = ". $ token. "</a> <br/> if you cannot click the above link, copy it to your browser's address bar to access the link. The link is valid for 24 hours. <Br/> if this activation request is not sent by you, ignore this email. <Br/> <p style = 'text-align: right'> -------- help house http://www.bkjia.com to top </p> "; $ rs = $ smtp-> sendmail ($ smtpemailto, $ smtpemailfrom, $ emailsubject, $ emailbody, $ emailtype); if ($ rs = 1) {$ msg = 'congratulations, registration successful! <Br/> Please log on to your mailbox and promptly activate your account! ';} Else {$ msg = $ rs;} echo $ msg ;}

Active. php

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. The status activation status changes to 1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.