How to send email verification link using php and ajax.

POSTED

AUTHOR: VARUN SINGH

Email verification link is the most important part on the web. Because every user has a unique email id. This is very important to ckeck users email id is fake or real. So that we can identify the users email id throug sending a verification link on the user's email id. We can not verify untill users click on verification link. So here I am going to explain how to send email verification link using php and ajax coding live without refreshing page. Now i am going to display the output of this tutorials.

Email verification link Page Output:

Here is the output of the Page. You can check your email is verified or not. Or you can submit your email to verify.

email verification link
download live view


Implementation of code:


email.sql: Import this file in database to create users table in email database . Or you can create manually users table in email datebase. Here is the code of this file.

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


--
-- Database: `email`
--

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

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

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `email` varchar(30) NOT NULL,
  `status` varchar(10) NOT NULL,
  `pass` varchar(80) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `username`, `email`, `status`, `pass`) VALUES
(1, 'varun', 'varun@gmail.com', '1', ''),
(2, 'lakshmi', 'lakshmi@gmail.com', '1', ''),
(3, 'rohit', 'rohit@gmail.com', '1', '');


config.php: This file is used to create a connection to the database to access data from database.

<?php
$con=mysqli_connect("localhost","root","","email");
mysqli_set_charset($con,'utf8');
?>

index.php: This file is used to create user interface of the tutorials.

<html>
	
	<title>How to send Email Verification link to verify email address. - coder glass</title>
	<head>
		<link type="text/css" rel="stylesheet" href="style.css" />
		<meta name="Description" content="How to send Email Verification link to verify email address using php and ajax."/>
		
        <meta name="Keywords" content=" email, verification, link,  live , check, free, tutorial, on, Java programming, jsp, ejb, html, css, xml,
		
	    twitter script,ecommerce,  Technologies, Free,  Tutorials, References, Examples,  coder glass, coderglas, coder"/>
		
        <link href='https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
		<link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
		<script src="jquery.min.js" type="text/javascript"></script>
<SCRIPT type="text/javascript">
$(document).ready(function()
{
$("#email").change(function() 
{ 
var email = $("#email").val();
var msgbox = $("#status");

if(email.length > 4)
{
$("#status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');

$.ajax({  
    type: "POST",  
    url: "ajax.php",  
    data: "email="+ email,  
    success: function(msg){  
   $("#status").ajaxComplete(function(event, request){ 
	if(msg == 'OK')
	{ 
	
	    $("#email").removeClass("red");
	    $("#email").addClass("green");
        msgbox.html('<img src="not-veryfied.png" align="absmiddle">');
	}  
	else  
	{  
	     $("#email").removeClass("green");
		 $("#email").addClass("red");
		msgbox.html('<img src="available.png" align="absmiddle">');
	}  
   
   });
   } 
   
  }); 

}
else
{
$("#email").addClass("red");
$("#status").html('<font color="#cc0000">Please enter atleast 5 letters</font>');
}
return false;
});

});
</SCRIPT>
		
		
	</head>
	<body>
	<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-73364464-1', 'auto');
  ga('send', 'pageview');

   </script>
		<center><div class="base">
			<div id="logo">
				<img src="logo.png" width="300" height="70" />
			</div>
			<div id="info1">
			How to send	Email verification link  using PHP and Ajax<br>
				Designed by Varun Singh
			</div>
			
			<div class="base2">
			<?php include('../../../advertise/left-ads.php'); ?>
			<div id="form2" style="float:left">
				<div id="form-img">
				<img src="profile-img.png" width="80" height="80" />
			    </div>
				<h2 class="title" style="text-align:center; font-size:25px; margin-top:50px;"><?php echo @$message; ?></h2>
				<div id="info4">
				  <b style="color:blue; font-weight:bold;">Already Verified Email:</b> &nbsp; (varun@gmail.com, lakshmi@gmail.com)
			    </div>
				<div >
				<div id="info5">
				  Please check here your Email is already verified  or not.
			    </div>
				<div >
				<input  style="width:300px; font-size:14px; height:40px;padding:10px; border:solid 1px #000;
				border-radius:5px 5px 5px 5px;" type="text" name="email" id="email" placeholder="Check Your Email is verified or not" />
				
                <span style="color:red;" id="status"></span>
				</div>
				<div>
				   <input type="submit" id="button2" value="Check" />
				</div>
				<br>
				<b style="color:#0abc34; font-weight:bold;">Please Verify Your Email Here.</b>
				<form action="index.php" method="POST" enctype="multipart/form-data">
				<div  >
				<input  style="width:300px; font-size:14px; height:40px;padding:10px; border:solid 1px #000;
				border-radius:5px 5px 5px 5px;" type="text" name="email1" id="email" placeholder="Verify Your Email here" />
				
				<input type="submit" name="verify" id="button3" value="Verify" /><br>
				<b style="color:#08a42c; ">We will send Email verification link on your Email Address. Then check your Email and click
				on verification link. After click on veryfication link, you will get verification message. Then check your Email is verified or not.</b>
				
				</div>
				</form>
                </div>
				
			</div>
			<?php include('../../../advertise/right-ads.php'); ?>
			</div>
			<div class="clear"></div>
			
			
			
			
		
		</div>
		</center>
	</body>
	
</html>
<?php
    include("config.php");
    if(isset($_POST['verify'])){
	    $email1 = $_POST['email1'];
        $pass = md5(uniqid(rand(), true));
		$status = 0;
		$query = "INSERT INTO users (email, status, pass) 
		values ('$email1', '$status', '$pass')";
		
		if(mysqli_query($con,$query)){
			
			$to = $email1;
			$subject = "Activation Link";
			$message = " Please Activate  account to given link. \n";
			$message .= "www.coderglass.com/php/demo/email-verification-link/index.php?email=$email1&passcode=$pass";
			$header = "From:varun@coderglass.com \r\n";
			mail($to,$subject,$message,$header);
			
		echo "<script>alert('Your Email verification Link has been sent to your email account. Please check your email to verify your Email. Thanks.')</script>";
		
		}
    }
?>

<?php
include("config.php");
if(isset($_GET['passcode'])){
				$p_code=$_GET['passcode'];
				$u_email=$_GET['email'];
				
				$v_query="Select email, pass, status from users where email='$u_email' AND pass='$p_code'";
				$v_run=mysqli_query($con, $v_query);
				$v_row=mysqli_fetch_array($v_run);
				$p_c=$v_row['pass'];
				$u_e=$v_row['email'];
				$u_s=$v_row['status'];
				if($p_c==$p_code and $u_e==$u_email and $u_s==0){
					
					$act=1;
					
					$user_code=$p_c;
					$update_query = "update users set status='$act' where pass='$user_code'";
		            $run_update = mysqli_query($con,$update_query);
					
					$message="Hurray, Now you are successfully verify";
					
					} 
					elseif($p_c==$p_code and $u_e==$u_email and $u_s==1) {
					$message="You are already verified.";
					}else{
					echo "<script>alert('Your activation link is incorrect, Please try once again your account.')</script>";
				    }
			}else{
			   
                $message="You have not veryfied yet, please verify.";			   
			}
				    
			

?>

ajax.php: This file is used to check email in the database.

<?php

include("config.php");



if(isset($_POST['email']))
{
$email = $_POST['email'];
$sql = mysqli_query($con,"select * from users where email='$email' and status=1");
if(mysqli_num_rows($sql))
{
echo 'Email is already in use.';
}
else
{
echo 'OK';
}
}

?>

The End:

Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.



Comments: