Use Php to write the instance code for Email activation verification after registration

Source: Internet
Author: User

A total of two pages are required: register. php and verify. php.

1. User Registration Form register. php

Copy codeThe Code is as follows:

<Body>

<Form action = "register. php" method = "post" name = "register">

Username: <input type = "text" name = "username"/>

Password: <input type = "password" name = "password"/>

Email: <input type = "text" name = "email"/>

<Input type = "submit" value = "register"/>

</Form>

</Body>

</Html>

2. Create a user data table Users

Copy codeThe Code is as follows: create table if not exists 'users '(

'Id' int (11) not null auto_increment,

'Status' varchar (20) not null,

'Username' varchar (20) not null,

'Password' varchar (20) not null,

'Email 'varchar (20) not null,

'Activationkey' varchar (100) not null,

Primary key ('id '),

Unique key 'username' ('username '),

Unique key 'email '('email '),

Unique key 'activationkey' ('activationkey ')

) ENGINE = MyISAM default charset = latin1 AUTO_INCREMENT = 9;

3. Create a verification code and store user registration information to the data table.
The status 'verify 'is used to indicate users that have not been activated.

Copy codeThe Code is as follows: $ activationKey = mt_rand (). mt_rand ();

$ Username = mysql_real_escape_string ($ _ POST [username]);

$ Password = mysql_real_escape_string ($ _ POST [password]);

$ Email = mysql_real_escape_string ($ _ POST [email]);

$ SQL = "INSERT INTO users (username, password, email, activationkey, status) VALUES ('$ username',' $ password', '$ email', '$ activationkey ', 'verify ')";

4. Send verification code

Copy codeThe Code is as follows: echo "An email has been sent to $ _ POST [email] with an activation key. Please check your mail to complete registration .";

# Send activation Email

$ To = $ _ POST [email];

$ Subject = "YOURWEBSITE.com Registration ";

$ Message = "Welcome to our website! \ R \ rYou, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link: \ rhttp: // www.YOURWEBSITE.com/verify.php? $ ActivationKey \ r \ rIf this is an error, ignore this email and you will be removed from our mailing list. \ r \ rRegards, \ YOURWEBSITE.com Team ";

$ Headers = 'from: noreply @ YOURWEBSITE.com '. "\ r \ n ".

'Reply-To: noreply @ YOURWEBSITE.com '. "\ r \ n ".

'X-Mailer: PHP/'. phpversion ();

Mail ($ to, $ subject, $ message, $ headers );

5. verify the activation code verify. php
If the verification code is the same, the user is activated.

Copy codeThe Code is as follows: $ queryString = $ _ SERVER ['query _ string'];

$ Query = "SELECT * FROM users ";

$ Result = mysql_query ($ query) or die (mysql_error ());

While ($ row = mysql_fetch_array ($ result )){

If ($ queryString = $ row ["activationkey"]) {

Echo "Congratulations! ". $ Row [" username "]." is now the proud new owner of a YOURWEBSITE.com account .";

$ SQL = "UPDATE users SET activationkey ='', status = 'activated' WHERE (id = $ row [id]) ";

If (! Mysql_query ($ SQL )){

Die ('error: '. mysql_error ());

}

// Here, the user has fully activated the account, and you can jump to the login page

}

} // End of while

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.