Sample code to activate verification with email after PHP registration

Source: Internet
Author: User
By using email to verify the activation method, you can effectively help prevent malicious spam and registered bots from accessing. Using PHP to write the registration email verification activation process is very simple, I believe you can learn within a few minutes.

A total of two pages, register.php and verify.php

1. User Registration Form register.php

The code is as follows:


2. Create User Data tables users

The code is as follows:

CREATE TABLE IF not EXISTS ' users ' (   ' id ' int (one) ' NOT null auto_increment,   ' status ' varchar () '   Rname ' varchar ' NOT NULL,   ' password ' varchar (a) NOT null,   ' email ' varchar (a) NOT null,   ' Activationkey ' varchar (+) 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 Verification Code user registration information into the data sheet
We use the status ' verify ' to represent users who have not yet been activated.

The code is as follows:

$activationKey =  Mt_rand (). Mt_rand (). Mt_rand (). Mt_rand (). Mt_rand (); $username = Mysql_real_escape_string ($_pos T[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 ', ' $a Ctivationkey ', ' verify ') ";

4. Send Verification Code

The code is as follows:

echo "an email had been sent to $_post[email] with an activation key. Please check the your mail to complete registration. "; # #Send activation Email $to      = $_post[email]; $subject = "yourwebsite.com registration"; $message = "Welcome to our We Bsite!\r\ryou, or someone using your email address, have 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 a error, ignore this e-mail and you'll be removed from our Maili ng 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.

The code is as follows:

 $queryString = $_server[' query_string '); $query = "SELECT * from users"; $result = m Ysql_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, you can jump the page to the interface after landing}}//End of while 
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.