Write the instance code _php instance of email activation verification after registering with PHP

Source: Internet
Author: User
Tags php and

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

1. User Registration Form register.php

Copy Code code as follows:

<body>

<form action= "register.php" method= "POST" name= "register" >

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

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

E-mail: <input type= "text" name= "email"/>

<input type= "Submit" value= "registered"/>

</form>

</body>

2. Create User Data table users

Copy 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 the authentication code the user registers the information to deposit the data table
We use the state ' verify ' to represent a user that has not yet been activated.

Copy 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 Code code as follows:

echo "An email has been sent to $_post[email] with a activation key. Please check your mail to complete registration. ";

# #Send Activation Email

$to = $_post[email];

$subject = "yourwebsite.com registration";

$message = "Welcome to We website!\r\ryou, or someone using your email address, has completed registration at Yourwebsite . com. Can complete registration by clicking the following link:\rhttp://www. Yourwebsite.com/verify.php? $activationKey \r\rif This is a error, ignore this email and you are 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 Activation Code verify.php
If the verification code is the same, the user is activated.

Copy 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" 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 of the interface

}

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