A total of two pages are required: register. php and verify. php.
1. User Registration Form register. php
Copy codeThe Code is as follows:
<Html>
<Body>
<Form action = "register. php" method = "post" name = "register">
Username: <input type = "text" name = "username"/>
Password: <input type = "password" name = "password"/>
Email: <input type = "text" name = "email"/>
<Input type = "submit" value = "register"/>
</Form>
</Body>
</Html>
2. Create a user data table Users
Copy codeThe 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.
Copy codeThe 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
Copy codeThe 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.
Copy codeThe 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