PHP implementation of user registration after the mailbox verification, activation account

Source: Internet
Author: User
Tags md5 encryption
When we register on many websites, when the registration is complete, the system will automatically send a mail to the user's mailbox, the content of this email is a URL link, the user needs to click to open this link to activate the previous account registered on the site. The membership function will not be used until activation is successful.

This article will combine examples to explain how to use Php+mysql to complete a registered account, send an activation email, verify activation account, and handle the URL link period.

Business process

1, the user submits the registration information.

2, write to the database, this time the account status is not activated.

3, the user name password or other identification character encryption constructs the activation identification code (you can also call the activation code).

4, the structure of the activation of the identification code URL sent to the user submitted mailbox.

5, the user login to the mailbox and click on the URL, to activate.

6. Verify the activation ID and activate the account if it is correct.

Preparing Data Sheets

User Information table field email is very important, it can be used to verify the user, retrieve the password, and even to the site can be used to collect user information for email marketing, the following is the User Information table T_user table structure:

CREATE TABLE IF not EXISTS ' t_user ' (   ' id ' int (one) not null auto_increment,   ' username ' varchar (+) ' NOT null Commen T ' username ',   ' password ' varchar (+) NOT null COMMENT ' password ',   ' email ' varchar (+) NOT null COMMENT ' mailbox ',   ' token ' Varc Har (+) NOT NULL COMMENT ' account Activation Code ',   ' token_exptime ' int (ten) NOT null COMMENT ' Activation code expiration ',   ' status ' tinyint (1) NOT NULL DEFAULT ' 0 ' COMMENT ' state, 0-inactive, 1-activated ',   ' regtime ' int (ten) not NULL COMMENT ' registration time ',   PRIMARY KEY (' id ')) engine=myisa M  DEFAULT Charset=utf8;

Html

Place a registration form on the page where the user can enter the registration information, including the user name, password, and mailbox.

<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>     <p><input type= "Submit" class= "BTN" value= "Submit Registration" ></p> </form>

For the user's input to do the necessary front-end verification, about the form validation function, we recommend that you refer to this site article: Examples of the application of the form validation plugin validation, this article on the front-end verification code skipped, in fact, there should be a page that requires users to repeatedly enter the password input box, temporarily lazy to skip this.

register.php

The user submits the registration information to register.php for processing. Register.php needs to complete two functions of writing data and sending mail.

First contains the necessary two files, connect.php and smtp.class.php, these two files are included in the download package provided outside, welcome to download.

Include_once ("connect.php");//Connect Database include_once ("smtp.class.php");//Mail Send class

We then filter the information submitted by the user and verify that the user name exists (the front end can also be verified).

$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 ' username already exists, please change another username ';     Exit }

We then encrypt the user's password to construct the activation ID:

$password = MD5 (Trim ($_post[' password ')); encrypted password $email = trim ($_post[' email '); Mailbox $regtime = time ();  $token = MD5 ($username. $password. $regtime); Created to activate the id $token _exptime = time () +60*60*24;//expires after 24 hours  $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, the $token is a constructed activation identification code, which is composed of the user name, password and the current time and MD5 encryption. $token _exptime is used to set the expiration time of the activation link URL, the user can activate the account during this time period, this example sets the activation valid within 24 hours. Finally, insert these fields into the data table T_user.

When the data is inserted successfully, the calling mail sending class sends the activation information to the user's registered mailbox, noting that the constructed activation identifier is composed of a full URL as the activation link when the user clicks, and the following is the detailed code:

if (mysql_insert_id ()) {$smtpserver = ""; SMTP server, such as: smtp.163.com $smtpserverport = 25; SMTP server port, generally $smtpusermail = ""; The user mailbox of the SMTP server, such as xxx@163.com $smtpuser = ""; The user account of the SMTP server xxx@163.com $smtppass = ""; The user password for the SMTP server $SMTP = new SMTP ($smtpserver, $smtpserverport, True, $smtpuser, $smtppass); Instantiate the message Class $emailtype = "HTML"; Letter type, text: text; Web page: HTML $smtpemailto = $email; Receiving mail party, this example is registered user's email $smtpemailfrom = $smtpusermail; Send mail party, such as xxx@163.com $emailsubject = "user account activation";//message header//message body content $emailbody = "Dear". $username. " :<br/> Thank you for registering a new account at my station. <br/> Please click on the link to activate your account. <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/> If the above link cannot be clicked, please copy it into your browser's address bar to access it, the link is valid within 24 hours.     ";     Send mail $rs = $smtp->sendmail ($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); if ($rs ==1){$msg = ' Congratulations, registration is successful! <br/> Please login to your mailbox to activate your account in time!         ';         }else{$msg = $rs; }} echo $msg;

There is also a fairly useful and powerful mail-sending class to share everyone: use Phpmailer to send a message with attachments and support HTML content, directly can use OH.

active.php

If there is no accident, you register your account when you fill in the email will receive a letter sent by Helloweba, this time you directly click on the activation link, to active.php processing.

active.php receives the submitted link information, gets the value of the parameter verify, which is the activation identifier. Compare it with the user information in the data table, if there is a corresponding data set, determine whether it expires, if within the validity period will be the corresponding User table field status Set 1, that is activated, so that the activation function has been completed.

Include_once ("connect.php");//Connect 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_fetc H_array ($query); if ($row) {     if ($nowtime > $row [' token_exptime ']) {//24hour         $msg = ' Your activation has expired, please log in to your account to 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 = ' activation succeeded! ';     } }else{     $msg = ' error. ';     } echo $msg;

After successful activation, it is not useful to find the token field and you can empty it. Next we will explain the user retrieve password function, also need to use the mailbox verification, please pay attention.

  • Related Article

    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.