New User Registration With Email Verification Using PHP and Mysqli - Mostlikers

02 November, 2015

New User Registration With Email Verification Using PHP and Mysqli

In this tutorial, we are going to see how to implement New user registration with email verification script. if you're using register form in a website, you need to use email verification because it will reduce spam register user and make sure you will get a correct registered user on your website.

New User Registration With Email Verification Using PHP and Mysqli


Live Demo        Download

Database

Create sample table called as 'users' and create columns that table.
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(100) NOT NULL,
  `name` varchar(250) NOT NULL,
  `password` varchar(250) NOT NULL,
  `status` enum('0','1') NOT NULL DEFAULT '0',
  `active_code` varchar(300) NOT NULL,
  PRIMARY KEY (`id`)
)


Database connection(config.php)

<?php  
   $user = 'root'; 
   $password = ''; 
   $database = 'mostlikers'; 
   $db = new mysqli('localhost',$user,$password,$database); 
 ?>


HTML

Create simple form element
<html>
<body>
  <form method="post">                          
  <label>Email *</label>
  <input type="text" name="email">
  <label>Password *</label>
  <input type="password" name="password">    
  <input type="submit" id="btn-login" value="Submit" name="form_submit">
  <div class="<?=(@$msg_sucess=="") ? 'error' : 'green' ; ?>">
  <?php echo @$msg; ?><?php echo @$msg_sucess; ?>
  </div> 
  </form> 
</body>
</html>

Index.php

Contain the form email value to check email id exist or not. if post email id not exists activate URL will generate to send via email.  
<?php
include("config.php");
if(isset($_POST['form_submit']))
{  
  extract($_POST);
  if($email!="" && $password!="") :
    $email=mysqli_real_escape_string($db,$email);
    $pwd=md5(mysqli_real_escape_string($db,$password));
    $emailval = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';    
    if(preg_match($emailval, $email)) :
      $db_check=$db->query("SELECT * FROM `users` WHERE email='$email'");
      $count=mysqli_num_rows($db_check);
      if($count< 1) :         
         $active_code=md5($email.time());
         $link = 'http://demos.mostlikers.com/php-email-verification/
         verify.php?key='.$active_code;         
         $fetch=$db->query("INSERT INTO users(email,password,active_code)
          VALUES('$email','$pwd','$active_code')");
         $to="$email"; //change to ur mail address 
         $reply = 'karthicktech77@gmail.com';
         $strSubject="Mostlikers | Email Verification";
         $message = '<p>Hi there!, <br/> Please verify your email
           get activate your account.</p>' ;              
         $message .= '<p>Click here : <a href="'.$link.'">'.$link.'</p>' ;              
         $headers = 'MIME-Version: 1.0'."\r\n";
         $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
         $headers .='Reply-To: '. $reply . "\r\n" ;
         $headers .= "Bcc: karthicktech77@gmail.com\r\n";
         $headers .= "From:Mostlikers info@mostlikers.com";         
         $mail_sent=mail($to, $strSubject, $message, $headers);  
         $msg_sucess="Registration has been successfully, Please verify your email.
         Check your inbox.The email may delay by few seconds, else check your spam or
         junk list.";
      else :
        $msg="This $email email address is already verified.";
      endif;  
    else :
      $msg="Please enter your valid email id";
    endif;      
  else : 
    $msg="Please fill all mandatory fields";
  endif;
unset($_POST);
}
?> 

$link - Email verification url link.
mail() - Php mailer functions

Verify.php

Contains PHP code users status will update to table 0 - inactive to 1- active 
<?php 
include("config.php");
if(@$_GET['key']!=""):
    $active_code=mysqli_real_escape_string($db,$_GET['key']);
    $fetch=$db->query("SELECT * FROM `users` WHERE `active_code` = '$active_code'");
    $count=mysqli_num_rows($fetch);
    if($count==1) :
      $row=mysqli_fetch_array($fetch);
      $user_id = $row['id']; 
      if($row['status']=='0'):
        $db->query("UPDATE users SET status ='1' WHERE id='$user_id'");
        $msg="Your account is activated successfully.";
      else :
        $msg="Your account has been already activated.";
      endif;
    else :
      $msg="Wrong activation code. Check your email activate again";
    endif;
else :
    header("Location:404.php");
endif;
echo $msg;
?>


Have a Question? Share your query by writing a comment below.

"The bird is powered by its own life and by its motivation. "
                                             
                                         - A. P. J. Abdul Kalam








Related Topics




19 comments:

  1. hi karthick
    how much you earned from your blog :)

    keep up the good work

    ReplyDelete
  2. will it be applied in localhost?

    ReplyDelete
  3. will it work on localhost?

    ReplyDelete
    Replies
    1. It will work. But you have to update local email setting configuration.

      Delete
  4. no mysql 'status'

    $db->query("UPDATE `users` SET `status` = '1' WHERE id='$user_id'");

    ReplyDelete
  5. The register code is working like it should be, no errors at all. Only, it seems from verifying, that i am always am activated.

    The status column is not updating at all, i even replaced the status column with a new one. I wonder only about this part of the code:

    if($row['active_code']=='0'):
    $db->query("UPDATE `users` SET `status` = '1' WHERE id='$user_id'");


    Does it not need to check the status column instead of active_code? Btw, is not $user_id wrong? Does is not need to check email is the same and then update the status column to 1?

    I might seen a bug there, that with pressing the activation link or before, that the active_code is filled in and might he said then, that he is already activated? I can be wrong here btw :)

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Can i ask, what did you fixed? Because i do not see it here and in de zip file. Thx btw for fast response.

      Delete
  6. Hi Jos,

    I have replaced if($row['active_code']=='0'): to if($row['status']=='0'): line

    I will update zip file ASAP.

    Thank you

    ReplyDelete
    Replies
    1. I only wonder about this little piece what it does: id='$user_id' in verify. Because, how far i can see it, i do not see an $user_id string, in both files (i might overlooked it somewhere).

      Delete
  7. Okay, i see that there need another fix :P

    Correct php code is for verify:

    query("SELECT * FROM `users` WHERE `active_code` = '$active_code'");
    $count=mysqli_num_rows($fetch);
    if($count==1) :
    $row=mysqli_fetch_array($fetch);
    if($row['status']=='0'):
    $db->query("UPDATE `users` SET `status` = '1' WHERE `active_code` = '$active_code'");
    $msg="Your account is activated";
    else :
    $msg="Your account is already activated";
    endif;
    else :
    $msg="Wrong activation code. Check your email activate again";
    endif;
    else :
    header("Location:404.php");
    endif;
    echo $msg;
    ?>

    I need to say this, if you have issues with updating the status column. Then simply delete that row and create another column with a different name.

    So if you have the sql file, it looks like this:

    CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `email` varchar(100) NOT NULL,
    `name` varchar(250) NOT NULL,
    `your column name` varchar(10) NOT NULL DEFAULT '0',
    `password` varchar(250) NOT NULL,
    `active_code` varchar(300) NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

    and everything shall work fine.



    Besides that, i like to say there is a bug on activate.php, where he shouts 2 times account is activated. 1) where it suppose to be and little textline, in left upper corner.

    BUT, i think this bug is a friendly bug ;)

    ReplyDelete
    Replies
    1. Thank you for your suggestions. It's really helpful our readers, I have changed if($row['status']=='0'): line. Now it's working fine. Demo and zip file i will update soon.

      Keep and touch with us.

      Delete
    2. I will Karthick :D
      Thx for updating the stuff, on the things i said :)

      Delete
    3. Demo and zip file data updated.

      Delete
  8. Erm, i just have one question doh. I like the modal one in the zip file. Only i like that centered on the page, then just hanging bit to the left. Was checking solutions out the web, but...i didn't found a worthy one. Because i making this for all kind of devices and for cleaning view, center looks always good ;)

    ReplyDelete