By using Email to verify the activation method, you can effectively prevent malicious Spam and registered robot access. It is very easy to use php to write an Email to verify activation after registration. I believe you will be able to learn it in just a few minutes. A total of two pages are required: register. php and verify. php.
1. user registration form register. php
The code is as follows:
2. create a user data table Users
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 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.
The 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
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! \ 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.
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 ());
}
// Here, the user has fully activated the account, and you can jump to the login page
}
} // End of while