Send Verification link on email after registration in PHP

Send Verification link on email after registration in PHP

/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/

Hi all , Welcome to shubhammaurya.com , Today we are going to discuss ,
How to Send Verification link on email after registration in PHP

LET START

So, make a database and paste the below code OR make your own.

-- phpMyAdmin SQL Dump
-- version 4.6.5.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Aug 24, 2017 at 01:42 PM
-- Server version: 10.1.21-MariaDB
-- PHP Version: 5.6.30

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `testdb`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `user_id` int(11) NOT NULL,
  `email` varchar(255) NOT NULL,
  `token_Code` varchar(255) NOT NULL,
  `userstatus` varchar(10) NOT NULL DEFAULT 'N',
  `joindate` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`user_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

So, To start first make a file in notepad and save it as dbconfig.php and paste the below code.

<?php

/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/

$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "testdb";

 try
 {
     $DBcon = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass); 
     $DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // echo "Done..";
 }
 catch(PDOException $e)
 {
     echo "ERROR : ".$e->getMessage();
 }

 include_once 'class.User.php';
 //$user=new USER($DBcon);
?>

Second Step make a file in notepad and save it as class.User.php and paste the below code.

<?php

/**
* 
*/
class USER 
{
	private $db;
	
//Constructor
	function __construct($DBcon)
	{
		$this->db=$DBcon;
	}



//Signup
	public function signup($email,$code)
	{

try {

$stmt=$this->db->prepare("INSERT into users(email,joindate,token_Code) VALUES(:email,  CURRENT_TIMESTAMP , :code )");

if($stmt->execute(array(':email'=>$email ,':code'=>$code)))
{
	return $stmt;
}

} catch (PDOException $e)
 {
	echo $e->getMessage();
}


	}




//Redirect
	public function redirect($url)
	{
		header("Location: $url");
		exit();
	}


//Send Mail
function send_mail($email,$message,$subject)
	{						
		require_once('mailer/class.phpmailer.php');
		$mail = new PHPMailer();
		$mail->IsSMTP(); 
		$mail->SMTPDebug  = 0;                     
		$mail->SMTPAuth= true;                  
		$mail->SMTPSecure="ssl";                 
		$mail->Host="smtp.gmail.com";      
		$mail->Port=465;             
		$mail->AddAddress($email);
		$mail->Username="Your Email id";  
		$mail->Password="Your Password";            
		$mail->SetFrom('Your Email id');
		$mail->Subject    = $subject;
		$mail->MsgHTML($message);
		$mail->Send();
		return true;
	}


//Get Last Inserted Id
	public function lastID()
	{
		$stmt = $this->db->lastInsertId();
		return $stmt;
	}


}
?>

Third Step make a file in notepad and save it as index.php and paste the below code.

<?php

  //connect to database
require_once 'dbconfig.php';

//Session Start
session_start();
$reg=new USER($DBcon);

//Registration
if(isset($_POST['register']))
{
$email=$_POST['email'];
$code=md5(uniqid(rand()));
try 
{

if($reg->signup($email,$code))
{
if(true)
{  
  $id=$reg->lastID($DBcon);

   $message = "  
 <div style='margin: auto;background-color: #fafafa;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);'>
 <div style='color: white;text-align: center;background-color: #2562c5;padding: 20px;'>CONFIRM REGISTRATION</div><br>      
            <div style='padding: 20px;color: #FF5722;'>Hello ,<br><span style='color: #FF5722;'>$email</span>,
            <br><br>
            Welcome $email<br>
            To complete your registration  please , just click the following link : <br/>
            <br>
            <a style='text-decoration: blink;background: #2562c5;color: #ffffff;padding: 10px;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);' href='http://localhost/email_Veri/verify.php?id=$id&code=$code'>Click Here to Activate Your Profile.</a>
            <br><br><br>
            <spa>Regards<span><br>
            <span>Team Shubhammaurya.com</span>
            </div>
            </div>";
            
      $subject = "Confirm Registration";

    if($reg->send_mail($email,$message,$subject))
    {
      if(true)
      {
       
    echo "<script>alert('Check your Mail to confirm')</script>";
      exit;
    }
  }
}

  // echo "<script type='text/javascript'>alert('Registration Done..!');</script>"; 
} 
  }

catch (PDOException $e) 
{
   echo $e->getMessage();
}
}

?>
<!DOCTYPE html>
<html>
<head>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
</head>
<body>

<div class="container">
  <div class="row">
    <div class="col-md-12 text-center">
    <div style="background: #00BCD4; padding: 60px;"> 
       <form  method="post"  autocomplete="off">  
   <input type="email" name="email" placeholder="Your Email" class="form-control" required="required" ><br>
     <input type="submit" name="register" value="Sign up" class="btn btn-danger" >
      </form>
      </div>
    </div>
  </div>
</div>


</body>
</html>

Fouth Step make a file in notepad and save it as verify.php and paste the below code.

<?php
require_once 'dbconfig.php';


if(empty($_GET['id']) && empty($_GET['code']))
{
	$user->redirect('index.php');
}


if(isset($_GET['id']) && isset($_GET['code']))
{
	$id = $_GET['id'];
	$code = $_GET['code'];
	
	$statusY = "Y";
	$statusN = "N";
	
	$stmt = $DBcon->prepare("SELECT user_id,userstatus FROM users WHERE user_id=:uID AND token_Code=: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 = $DBcon->prepare("UPDATE users SET userstatus=:status WHERE user_id=:uID");
			$stmt->bindparam(":status",$statusY);
			$stmt->bindparam(":uID",$id);
			$stmt->execute();	
			
			$msg = "

			<div style='/* text-decoration: blink; */background: #2562c5;color: #ffffff;padding: 20px;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);text-align: -webkit-center;width: 50%;margin: auto;'><span><strong>WoW !</strong>  Your Account is Now Activated : &nbsp;&nbsp;<a style='background-color: white;text-decoration: blink;padding: 5px;margin: auto;' href='../index.php'>Click here to Login</a></span><div>";	
		}
		else
		{
			$msg = "

			<div style='/* text-decoration: blink; */background: #2562c5;color: #ffffff;padding: 20px;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);text-align: -webkit-center;width: 50%;margin: auto;'><span><strong>Sorry !</strong>  Your Account is already Activated : &nbsp;&nbsp;<a style='background-color: white;text-decoration: blink;padding: 5px;margin: auto;' href='../index.php'>Click here to Login</a></span><div>";
		}
	}
	else
	{
		$msg = "

		<div style='/* text-decoration: blink; */background: #2562c5;color: #ffffff;padding: 20px;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);text-align: -webkit-center;width: 50%;margin: auto;'><span><strong>Sorry !</strong>  No Account Found : &nbsp;&nbsp;<a style='background-color: white;text-decoration: blink;padding: 5px;margin: auto;' href='../index.php'>Click here to Register</a></span><div>
			   ";
	}	
}

?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
	 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="jumbotron text-center" style="margin-top: 145px;">
<?php if(isset($msg)) { echo $msg; } ?>
</div>
</body>
</html>

Find Code at github : click here

Download Code From Here : click here

Comment Below, If any problem occurs.

STAY CONNECTED FOR MORE

 

By jackmaurya121

Hi , My Name is Shubham Maurya and i am currently working as a Project Manager.

27 thoughts on “Send Verification link on email after registration in PHP”
  1. Good day! Do you know if they make any plugins to help with SEO?
    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success.
    If you know of any please share. Thank you!

  2. We are a group of volunteers and starting a new scheme in our community.
    Your website provided us with valuable info to work on. You’ve done
    an impressive job and our entire community will be thankful to you.

  3. Nice post. I learn something totally new and challenging on websites I stumbleupon on a daily basis.
    It will always be useful to read through content from other authors and use something from their web sites.

  4. I do not even know how I ended up here, but I thought this post was great.

    I do not know who you are but certainly you’re going to
    a famous blogger if you aren’t already 😉 Cheers!

  5. With havin so much content do you ever run into any
    problems of plagorism or copyright infringement? My blog
    has a lot of exclusive content I’ve either authored myself
    or outsourced but it looks like a lot of it is popping it up all over the internet without my authorization. Do you know any ways to help stop content from being stolen? I’d certainly appreciate it.

  6. I’ve been surfing on-line greater than 3
    hours nowadays, but I by no means discovered any attention-grabbing article like yours.

    It is beautiful worth enough for me. In my view, if
    all webmasters and bloggers made good content as you probably did, the web might be a
    lot more useful than ever before.

  7. An impreѕsive share! I’ve just forwarded this onto a colleague who
    was conducting a little һomework on this. And he in fact ordered me
    breakfast due to the fact that I stumbled upon it
    for him… lol. So let mе reword this….
    Thank YОU for the meal!! But yeah, thanx for spending some time to talk about this matter hеre on your internet site.

  8. I know this web site offers quality depending articles or reviews and other information, is there any other
    site which gives these kinds of stuff in quality?

Leave a Reply