User Account Activation using email Verification using Php

How to Create Basic User Account Activation using email Verification using Php

This tutorial demonstrate to build an email verification script .Email verify is a php script that allows you to verify email address without storing anything in any databases.it will send users an email requiring them to 
click a link to verify their email before  and then your account will be activated...
   And then first, we need to establish a database connection and create a table to insert the account data. So let's go to PHPMyAdmin and create a new database with the name "dbtest" create a user account that has access to that database in order to insert and update data.
download phpmailer form an this link PHPMailer  ..
next,create a table and named as users....

Database: 
create table users(userID int primay key auto_increment not null,userName varchar(30) not null,userEmail varchar(30) not null unique,userPass varchar(30) not null,mdcode varchar(100));
and next ,we have to create empty .php file and named as
1.db.php
2.signup.php
3.user.php
4.verify.php
5.index.php
6.home.php
7.logout.php

1.db.php



<?php
    class db
    {
      private $host='localhost';
      private $uname='root';
      private $password='';
      private $dbname='dbtest';
      public $conn;
      public function dbconnection()
{
    $this->conn=null;
 try
    {

    $this->conn=new PDO('mysql:host='.$this->host.';dbname='.$this->dbname,
    $this->uname,$this->password);
    $this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  }
catch(PDOException $e){echo "connection error".$e->getMessage();}
  return $this->conn;
   } 
 }
?>

2.signup.php



<?php

session_start();

require_once 'user.php';

$user=new USER();

if($user->is_logged_in()!="")

{

 $user->redirect('home.php');

}

if(isset($_POST['submit']))

{

 $uname=trim($_POST['username']);

 $email=trim($_POST['email']);

 $pass=trim($_POST['password']);

 $code=md5(uniqid(rand()));

 $stmt=$user->query("select * from users where userEmail=:emailid");

 $stmt->execute(array("emailid"=>$email));

 $row=$stmt->fetch(PDO::FETCH_ASSOC);

 if($stmt->rowCount() > 0)

 {

  $msg="<strong>sorry! </strong> email is already taken try another..";

 }

 else

 {

  if($user->register($uname,$email,$pass,$code))

  {

   $id=$user->lastid();

   $key=base64_encode($id);

   $id=$key;

   $message="

      hello $email.<br/><br/>

      welcome to blogger<br/>

 <a href='http://localhost/testsignup1/verify.php?userID=$id&mdcode=$code'>click here to acitvate</a>";

      $subject="confirm registration";

      $user->sendmail($email,$message,$subject);

        $msg = "

                            <strong>Success!</strong>  We've sent an email to $email.

                    Please click on the confirmation link in the email to create your account. 

    ";

    

  }

  else

  {

   echo "sorry...error may occured";

  }

 }

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>



<body>

<?php if(isset($msg)) echo $msg; ?>

<form method="post">

   <input type="text" name="username" placeholder="username" /><br />

   <input type="email" name="email" placeholder="Email" /><br />

   <input type="password" name="password" placeholder="password" /><br />

   <input type="submit" name="submit" value="submit" /><br />

</form>
</body>

</html>


 $id=$user->lastid()-this condition check the lastid in user.php and there is an condition lastInsertId() get the userid  assign to $id
so we create a variable named as $key and assinged ($id)  is encode with base64 it was unique to find and covert very easy in email verify ..so base 64 the value is like MTU or uTT 
and then $key is assigned to $id....its $id and then $code is sended to sendmail() function to mail id and when you click to verify by mail the account will be activated....

3.users.php


<?php

require_once 'db.php';

class USER

{

 private $conn;

        public function __construct()

 {

  $database=new db();

  $db=$database->dbconnection();

  $this->conn=$db;

 } 

 public function query($sql)

 {

  $stmt=$this->conn->prepare($sql);

  return $stmt;

 }

 public function lastid(){

     $stmt=$this->conn->lastInsertId();

  return $stmt; 

  }

 

 public function login($email,$password)

 {

   try

  {

 $stmt = $this->conn->prepare("SELECT * FROM users WHERE userEmail=:email_id");

   $stmt->execute(array(":email_id"=>$email));

   $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

   

   if($stmt->rowCount() == 1)

   {

    if($userRow['userStatus']=="Y")

    {

     if($userRow['userPass']==md5($password))

     {

      $_SESSION['userSession'] = $userRow['userID'];

      return true;

     }

     else

     {

      header("Location: index.php?error");

      exit;

     }

    }

    else

    {

     header("Location: index.php?inactive");

     exit;

    } 

   }

   else

   {

    header("Location: index.php?error");

    exit;

   }  

  }

  catch(PDOException $ex)

  {

   echo $ex->getMessage();

  }

 }

 

  public function redirect($url)

 {

  header("location:$url");

 }

  public function is_logged_in()

      {

        if(isset($_SESSION['userSession']))

        {

        return true;

       }

      }

  public function register($uname,$email,$pass,$code)

 {

  try{

  $password=md5($pass);

  $stmt=$this->conn->prepare("insert into users (userName,userEmail,userPass,mdcode) values(:uname,:email,:pass,:code)");

  $stmt->bindparam(':uname',$uname);

  $stmt->bindparam(':email',$email);

  $stmt->bindparam(':pass',$password);

  $stmt->bindparam(':code',$code);

  $stmt->execute();

  return $stmt;

  }

catch(PDOException $e){echo "error:".$e->getMessage();}

  }

 public function logout()

 {

  session_destroy();

  $_SESSION['username'] = false;

 }

 

 function sendmail($email,$message,$subject)

 {      

  require_once('mailer/class.phpmailer.php');

  require_once('mailer/class.smtp.php');

  $mail = new PHPMailer;

  $mail->IsSMTP(); 

  $mail->SMTPDebug  = 2;                     

  $mail->SMTPAuth   = true;                  

  $mail->SMTPSecure = "tls";                 

  $mail->Host       = "smtp.gmail.com";      

  $mail->Port       = 587;       

  $mail->isHTML(true);        

  $mail->AddAddress($email);

  $mail->Username="chandra2555@gmail.com";  

  $mail->Password="chandra2555";            

  $mail->SetFrom('chandra2555@gmail.com','chandra Blogspot');

  $mail->Subject    = $subject;

  $mail->MsgHTML($message);

  if(!$mail->Send())

  {

   echo "message is not sent";

}

else{echo "message is sent to your email id";}



 } 

}

?>


     sendmail()-send mail is so complicated think ....lot of people having SMTP() failed and show blank and ssl is not working and tls is not working and port assigned is showing an error lot of problem facing but you are using a membership website it will works very easy but you have to free webhost it not work because hosting is not provide the permission to uses your gmail mail id ....what to do ....follow my instruction ...
once again......am using xampp for local sever and download phpmailer from above link i mention on top ...and then get sample code form same website ...
and then error SMTP() can occur only adding class.phpmailer.php  so add another file class.smtp.php in phpmailer it can solve the error and you got msg mail send successfully and then all before you have to do some changes  on your emailid see on below image 
    login gmail ->myaccount->connected apps and sites ->less secure app->on that's it .....
User Account Activation using email Verification using Php
User Account Activation using email Verification using Php

4.Verify.php


<?php

require_once 'user.php';

$user = new USER();



if(empty($_GET['userID']) && empty($_GET['mdcode']))

{

 $user->redirect('index.php');

}



if(isset($_GET['userID']) && isset($_GET['mdcode']))

{

 $id = base64_decode($_GET['userID']);

 $code = $_GET['mdcode'];

 $statusY = "Y";

 $statusN = "N";

 $stmt = $user->query("SELECT userID,userStatus FROM users WHERE userID=:uID AND mdcode=:code LIMIT 1");

 $stmt->execute(array(":uID"=>$id,":code"=>$code));

 $row=$stmt->fetch(PDO::FETCH_ASSOC);

 if($stmt->rowCount() > 0)

 {

  if($row['userStatus']==$statusN)

  {

   $stmt = $user->query("UPDATE users1 SET userStatus=:status WHERE userID=:uID");

   $stmt->bindparam(":status",$statusY);

   $stmt->bindparam(":uID",$id);

   $stmt->execute(); 

   

   $msg = "

             

       <strong>Awesome !</strong>  Your Account is Now Activated : 

<a href='index.php'>Login here</a>

         

          "; 

  }

  else

  {

   $msg = "

             

       <strong>sorry !</strong>  Your Account is already Activated : 

<a href='index.php'>Login here</a>

        

          ";

  }

 }

 else

 {

  $msg = "

         

      <strong>sorry !</strong>  No Account Found : <a href='signup.php'>Signup here</a>

   

      ";

 } 

}



?>

<!DOCTYPE html>

<html>

  <head>

    <title>Confirm Registration</title>

    

  </head>

  <body id="login">

    <div class="container">

  <?php if(isset($msg)) { echo $msg; } ?>

    </div>
</body>

</html>

5.index.php



<?php

session_start();

require 'user.php';

$login=new USER();

if($login->is_logged_in()!="")

{

 $login->redirect('home.php');

}

if(isset($_POST['submit']))

{

 $email=trim($_POST['email']);

 $password=trim($_POST['password']);

    if($login->login($email,$password))

 {

  $login->redirect('home.php');

 }



}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>



<body>

<?php

 if(isset($_GET['inactive']))

  {

   ?>

            

    <strong>Sorry!</strong> This Account is not Activated Go to your Inbox and Activate it. 

            <?php

  }

  ?>



<form method="post">

    <?php

        if(isset($_GET['error']))

  {

   ?>

            

    <strong>Wrong Details!</strong> 

   </div>
<?php

  }

  ?>

 <input type="text" name="email"  placeholder="email" />

  <input type="password" name="password" placeholder="password" />

  <input type="submit" name="submit" value="submit"/>

  <a href="signup.php" >signup</a>

</form>
</body>

</html>



6.Home.php


<?php

session_start();

require_once 'user.php';

$user=new USER();

if(!$user->is_logged_in()){

    $user->redirect('index.php'); 

 }





$stmt=$user->query("select * from users where userID=:emailid");

$stmt->execute(array(":emailid"=>$_SESSION['userSession']));

$row=$stmt->fetch(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>



<body>

hi      <?php echo $_SESSION['userSession'];?>      <?php  echo $row['userEmail'];?>   <a href="logout.php">logout</a>

</body>

</html>


7.logout.php


<?php

session_start();

require_once 'user.php';

$user = new USER();



if(!$user->is_logged_in())

{

 $user->redirect('index.php');

}



if($user->is_logged_in()!="")

{

 $user->logout(); 

 $user->redirect('index.php');

}

?>



In this step we get the User Account Activation using email Verification using Php. That's all, this is how to User Account Activation using email Verification using Php. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
Previous Post Next Post