Use PHP to write an instance code _php instance of email activation verification after registration

Source: Internet
Author: User
A total of two pages, register.php and verify.php

1. User Registration Form register.php

Copy the Code code as follows:

2. Create User Data tables users
Copy the Code code as follows:
CREATE TABLE IF not EXISTS ' users ' (

' id ' int (one) not NULL auto_increment,

' Status ' varchar (not NULL),

' username ' varchar (not NULL),

' Password ' varchar (not NULL),

' Email ' varchar (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.

Copy the Code code as follows:
$activationKey = Mt_rand (). Mt_rand (). Mt_rand (). 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 ', ' $a Ctivationkey ', ' verify ') ";

4. Send Verification Code
Copy the Code code 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 website!\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.

Copy the Code code 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, you can jump to the page after the landing interface

}

}//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.