New User Registration With Email Verification Using PHP and Mysqli

In this post i have explain about how to implement New user registration with email verification script. if your using register form in a website, you need to use email verification because it will be reduce spam register user and make sure you will get correct registered user in your website.

New User Registration With Email Verification Using PHP and Mysqli

Live Demo        Download

Database

Create sample table called as 'users' and create below 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,
  `active_code` varchar(300) NOT NULL,
  PRIMARY KEY (`id`)
)


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 exist activate url will generate to send via email.  
<?php
$db = new mysqli('localhost', 'root', '', 'mostlikers');
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://mostlikers.2my4edge.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
      $strSubject="Mostlikers | Email Verification";
      $message = '<p>Hi, 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 .= "From: info@mostlikers.com";            
      $mail_sent=mail($to, $strSubject, $message, $headers);  
      $msg_sucess="Registration successful, please verify your email.";
    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;
}
?> 


Verify.php

Contains PHP code users status will update to table 0 - inactive to 1- active 
<?php 
$db = new mysqli('localhost', 'root', '', 'mostlikers');
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);
  if($row['active_code']=='0'):
    $db->query("UPDATE `users` SET `status` = '1' WHERE id='$user_id'");
    $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;
?>


other download link

Comments

Popular posts from this blog

Script For Login, Logout and View Using PHP, MySQL and Bootstrap

Hyperledger Development with in 10 days — Day 6

Insert CheckBox and Radio button Data in MySQL Database Using PHP