Use Php to write the instance code for the Email activation validation after registration

  • 2020-05-30 19:44:07
  • OfStack

There are two pages, register.php and verify.php

1. User registration form register.php

   <form action="register.php" method="post" name="register"> 
       User name: <input type="text" name="username" /> 
       Password: <input type="password" name="password" /> 
       Email: <input type="text" name="email" /> 
      <input type="submit" value=" registered " /> 

2. Create the user data form 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`) 

3. Create verification code and store the user registration information into the data table
We use the state 'verify' for users who are not yet active.

 $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

 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 = " Registration"; 
 $message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at You can complete registration by clicking the following link:\r$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ Team"; 
 $headers = 'From: noreply@' . "\r\n" .   
     'Reply-To: noreply@' . "\r\n" .   
     'X-Mailer: PHP/' . phpversion();   
 mail($to, $subject, $message, $headers); 

5. Verify the activation code verify.php
If the captcha code is the same, the user is activated.

 $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 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 and you can redirect the page to the login screen    
   } // end of while 

Related articles: