Coding, PHP & MySQL

Members Registration With Email Verification Using PHP And MYSQL

The concept is simple. As a first step user will be asked for email address. Then an email will be sent at specified email address to verify the email. The user will click on the link in the email to go to the next step. Here user will enter the remaining details. These details will be added to MYSQL. The user will get the thank you page.

STEP 1: Email Verification Step

Here the user is asked for the valid email address. This is the step 1 of registration. An instant email will be sent at the specified email address. User will be asked to click the activation link in the email to complete the registration. So in short, this step is verifying the user’s email address.

This is step_1.html . This Html page is just asking the first name and email address of the user.

<html>
<head>
<title>Email Verification... STEP 1</title>
<meta name="robots" content="index,follow" />

</head>
<body>
<table width="600" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#fff">
  
  <tr>
    <td><p align="center">Email Verification - Step One</p>
      <blockquote>
        <blockquote>
          <p class="description">Please provide your full name and correct email address in the
           spaces below. You will receive an email at the email address you provide. Follow the 
           instructions in the email to complete the application process. Thank you.</p>
        </blockquote>
      </blockquote>
      <table border="0" cellspacing="2" cellpadding="2" align="center" width="420" bgcolor="#fff">
        <form method="post" name="form1" action="step2.php">
          <tr> 
            <td >Full Name<span class="attention">*</td>
            <td><input type="text" name="fname" size="30" /> </td>
          </tr>
        <!-- <tr> 
            <td><b >Last Name<span class="attention">*</b></td>
            <td><input type="text" name="lname" size="30" /> </td>
          </tr> -->
          <tr> 
            <td class="text_bold">Email<span class="attention">*</td>
            <td><input type="text" name="email" size="30" /> </td>
          </tr>
          <tr> 
            <td colspan="2" height="17"></td>
          </tr>
          
        </form>
      </table></td>
  </tr>
  
</table>
</body>
</html>

STEP 2: Email Sent To The User

Now the email is sent to the above user with an activation link. This is step2.php

  1. The name and email of the user are retrieved from the POST array.
  2. The values of name and email address are encoded using the base64 encoding function. This is to ensure the security.
    <?php
    // getting the name and email from the POST Array
    $first_name = $_POST['fname'];
    //$last_name = $_POST['lname'];
    $email = $_POST['email'];
    
    // Encoding the information
    $first_name_code = base64_encode($first_name);
    //$last_name_code = base64_encode($last_name);
    $email_code = base64_encode($email);
    ?>
    
  3. Now the email is sent using the mail function of PHP. The email is sent having the activation link. This link is the link of step3.php.
    The link has 2 informations attached. The name of user and first name of the user. Both values are encoded for security purpose.

    <?php 
            
            $semi_rand = md5(time());
            $email_txt  = "$first_name ,<br><br> 
            Thank you for your interest.
             To   continue with the application process, please click on the link below and be sure it all appears on one line 
    in the address line of your browser.
            <br><br> <a  href='http://www.yoursite.com/step3.php?ref_id=$email_code&ref_fname=$first_name_code'>
    http://www.yoursite.com/step3.php?ref_id=$email_code&ref_fname=$first_name_code</a> 
             <br><br>    
            ";    
                
                             
            $email_txt  = stripslashes($email_txt);
         
             $to = "$email";
             $subject = "Complete Application Process At yoursite.com";//
             $email_from = "application@yoursite.com";
        
             $headers = "From: ".$email_from;
             $headers .= "nMIME-Version: 1.0n" .
                            "Content-Type: multipart/mixed;n" .
                        " boundary="==Multipart_Boundary_x{$semi_rand}x"";
        
            $message1 = "This is a multi-part message in MIME format.nn" .
                          "--==Multipart_Boundary_x{$semi_rand}xn" .
                         "Content-Type:text/html; charset="iso-8859-1"n" .
                          "Content-Transfer-Encoding: 7bitnn" . $email_txt . "nn";
            if (@mail($to, $subject, $message1, $headers))
                {
                      $sent = "yes"; 
                     
                }
            else
                  {    
                    $sent = "no"; 
                    
                }
    ?>
    
  4. Now the HTML part of this page is displaying the greeting message telling the user to click on the activation link in the email.
    <html>
    <head>
    </head>
    <body bgcolor="#660000" link="9933FF" vlink="3333FF" alink="FF0033">
    <table width="600" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">
    <?php if($sent=="yes") {?>
      <tr>
        <td><p align="center" class="attention">Thank you for interest in yoursite.com </p>
          <blockquote>
            <blockquote>
              <p class="description">An email has been sent to the email you
                provided (<?php echo $email; ?>). Please follow the instructions in the email to
              continue with the application process.
                
              </p>
            </blockquote>
          </blockquote>
           </td>
      </tr> 
       <?php } ?>
      
     <?php if($sent=="no") {?>
      <tr>
        <td><p align="center" class="aoh_title"><span class="attention">Thank
              you for interest in yoursite.com</span></p>
          <blockquote>
            <blockquote>
              <p class="description">An error occurred when sending the verification email to you at (<?php echo $email ;?>) . 
                  Please go back and check the email for correctness or enter a different email address.  We are sorry for the inconvenience.  You can also opt to try again later.
    
                    
              </p>
            </blockquote>
          </blockquote>
           </td>
      </tr> 
       <?php } ?>
    </table>
    </body>
    </html>
    

STEP 3: Getting The Other Info Of The User After Email Verification

Now suppose that the user entered the valid email address. Then the user had got the email. User had clicked the activation link. Now the user will be on the step3.php page.

This page will now collect the user’s other info for membership completion. We will ask now for the last name and country only from the user. You can add the other details you want to ask from the user for your application. Let us explore how all this is done here.

  1. Here the email address and first name of the user are got from the link. As we had encoded these values, we need to decode them. So we are using the base64_decode function to get the original first name and email address of the user.
    <?php 
    // getting the email address of the user
    if(isset($_GET['ref_id'])) {
               $email = base64_decode($_GET['ref_id']);
        
    }
    
    
    //getting the first name of the user 
    if(isset($_GET['ref_fname']) && !($_GET['ref_fname']=="") ) {
                    $first_name = base64_decode($_GET['ref_fname']);
    }
    ?>
    
  2. Now a simple Form is displayed that will ask for the first name, last name, email address, password and country of the user. The text fields of email address and first name are already filled by the values, since we know them from the above code.

    This form is simply sending these values to the step4.php file.

    <html>
    <head>
    </head>
    <body bgcolor="#660000" link="9933FF" vlink="3333FF" alink="FF0033">
    <form name="form1" method="post" action="step4.php">
    <table width="600" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">
    
    <tr class="table_text">
                <td width="218" >First  Name*</td>
                <td width="245" bordercolor="#993399"><input type="text" name="first_name" size="30" value="<?php echo $first_name;?>" disabled>            </td>
              </tr>
    
    
    <tr class="table_text">
                <td width="218" >Last  Name*</td>
                <td width="245" bordercolor="#993399"><input type="text" name="last_name" size="30">            </td>
              </tr>
    
    <tr class="table_text">
                <td class="table_text">Email*</td>
                <td bordercolor="#993399"><input type="text" name="email" size="30" value="<?php echo $email; ?>" disabled> </td>
    </tr>
    
    <tr class="table_text">
                <td width="218" >Password*</td>
                <td width="245" bordercolor="#993399"><input type="password" name="password" size="30">            </td>
       </tr>
    
    <tr class="table_text">
                <td width="218" >Country*</td>
                <td width="245" bordercolor="#993399"><input type="text" name="country" size="30">            </td>
       </tr>
    
    <tr><td><input type="submit" name="Submit" value="Submit"></td></tr>
       </table>
    </form>
    

STEP 4: Storing The User Info Into MYSQL Database

Now we have everything got. This is the last step.

We will store the user info into the database.

  1. From the POST array collect user information. These are first name, last name, email address, password, country.
    <?php
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $country = $_POST['country'];
    
    ?>
    
  2. Now we have to store this info in our MYSQL database. We have assumed that you have a table named users with the fields first name, last name, email address, password, country.

    Following query will simply add the above user info in the users table. A congratulation message will be displayed on successfull registration.

    <?php
    
    $query = "INSERT INTO users VALUES(NULL, '$first_name', '$last_name', '$email', '$password', '$country')";
    
    $result = mysql_query($query);
    
    if($result) echo "The new user is added to the database successfully"; else echo "The user was not added due to some error";
    
    ?>
    

I hope you will find the steps in this tutorial very easy to follow and usefull.