Use Php to write example code for email activation verification after registration_PHP tutorial

WBOY
Release: 2016-07-21 15:12:10
Original
1122 people have browsed it

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

1. User registration form register.php

Copy code The code is as follows:

Username:

Password:

Email:

2. Create user data table Users

Copy code The 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 verification code and store user registration information in the data table
We use the status 'verify' to represent users who have not yet been activated.

Copy code The code is 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', '$activationKey', 'verify')";

4. Send verification code

Copy code The 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!rrYou, 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?$activationKeyrrIf this is an error, ignore this email and you will be removed from our mailing list.rrRegards, YOURWEBSITE.com Team";

$headers = 'From: noreply@ YOURWEBSITE.com' . "rn" .

'Reply-To: noreply@ YOURWEBSITE.com' . "rn" .

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

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

5. Verify the activation code verify.php
If the verification codes are the same, activate the user.

Copy code The 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());

}                                                                      

// At this point, the user has fully activated the account, you can jump to the page after login

}

} // end of while

http://www.bkjia.com/PHPjc/326740.html

truehttp: //www.bkjia.com/PHPjc/326740.htmlTechArticleA total of two pages are required, register.php and verify.php 1. User registration form register.php copy code code As follows: html body form action="register.php" method="post" name="register"...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!