PHP implementation of user registration activation mailbox function sample code

Source: Internet
Author: User
Tags md5 encryption
This article mainly introduces the implementation of PHP Activation user Registration Verification mailbox function, detailed analysis of the PHP mail activation user involved in the database, mail-related operations skills, the need for friends can refer to the following

This article describes the PHP implementation of the activation of user registration verification mailbox function. Share to everyone for your reference, as follows:

Here is an example of how to use Php+mysql to complete a registered account, send an activation email, verify the activation account, and handle the URL link period.

Register for Mailbox Activation process

1. User Registration
2, insert user data, the account is not activated at this time.
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.

T_user.sql

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 (five) NOT null COMMENT ' user name ', ' Password ' varchar (+) NOT null COMMENT ' password ', ' email ' varchar (+) NOT null COMMENT ' mailbox ', ' token ' varchar (NOT NULL COM ment ' account Activation Code ', ' token_exptime ' int (ten) NOT null COMMENT ' Activation code expiration ', ' status ' tinyint (1) NOT null DEFAULT ' 0 ' COMMENT ' state, 0-Non-excited Live, 1-activated ', ' regtime ' int (ten) not NULL COMMENT ' registration time ', PRIMARY KEY (' id ')) engine=myisam DEFAULT Charset=utf8;

Html

The following is a registration form that allows users to enter registration information, including user names, passwords, and mailboxes.

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

register.php finish writing data and sending mail

First connect the database and include the Mail send class smtp.class.php

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

Front-end verification form we omitted, directly to see the program

$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 ')); Encryption Password: ($_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 ()) {//write succeeded, email include_once ("smtp.class.php"); $smtpserver = "smtp.163.com"; SMTP server $smtpserverport = 25; SMTP Server Port $smtpusermail = "hjl416148489_4@163.com"; The user mailbox of the SMTP server $smtpuser = "hjl416148489_4@163.com"; The user account of the SMTP server $smtppass = "hjl7233163"; The user password for the SMTP server $SMTP = new SMTP ($smtpserver, $smtpserverport, True, $smtpuser, $smtppass);  A true in this case is that authentication is used, otherwise it is not used. $emailtype = "HTML";  Letter type, text: text; Web page: HTML $smtpemailto = $email;  $smtpemailfrom = $smtpusermail;  $emailsubject = "user account activation"; $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.jb51.net/demo/active.php?verify= '. $token. "' target= ' _blank ' >http://www.jb51.net/demo/active.php?verify=". $token. </a><br/> If the above link cannot be clicked, copy it into your browser's address bar to access it, which is valid for 24 hours. <br/> If this activation request is not issued by you, please ignore this email.  <br/><p style= ' text-align:right ' >--------Script House http://www.jb51.net to </p> "; $rs = $smtp->sendmail ($smtpemaIlto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); if ($rs = = 1) {$msg = ' Congratulations, your registration is successful! <br/> Please login to your mailbox to activate your account in time!  ';  } else {$msg = $rs; } Echo $msg;}

active.php

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_fetch_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. And the status activation state becomes 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.