Email Verification on User Registration In PHP

Hello guys, this post is about email verification system for new user registration. You would have seen 99.9% of websites and web applications want you to verify your email upon registration. Only users with verified email id’s will be able to login. Its really very important to verify email id’s before any user can login into your system. There are various ways to verify users email address, I will demonstrate one of them.Its very simple to implement. There are various test cases that should be kept in mind when creating a email verification system for new user registration. I have tried to cover all the scenarios, If you find any case which I have missed then do let me know I will surely Improve the script. Below is a simple flowchart:

email verification flowchart


I have created one table “REGISTER” to store user’s name, email and password. This table also contains two more columns “IsActive” and “Hash”. IsActive can have two values 1 or 0 depending on email id is verified or not respectively. Hash field stores MD5 hash which is sent to user’s email for verification.

Register Table Schema

I have created “register.php” which is responsible for entering new users information in REGISTER table and send email to user’s given email id for verification. As soon as user clicks the ACTIVATE BUTTON in his email, “verify.php” does its job by activating this email and now user can login into our application.

Code for register.php:


$name = trim(mysql_escape_string($_POST['name']));
$email = trim(mysql_escape_string($_POST['email']));
$passwords = trim(mysql_escape_string($_POST['pwd']));
$password = md5($passwords);

$query_verify_email = "SELECT * FROM register WHERE email ='$email' and isactive = 1";
$verified_email = mysqli_query($con,$query_verify_email);
if (!$verified_email) {
echo ' System Error';
if (mysqli_num_rows($verified_email) == 0) {
// Generate a unique code:
$hash = md5(uniqid(rand(), true));
$query_create_user = &quot;INSERT INTO <code>register</code> ( <code>name</code>, <code>email</code>, <code>password</code>, <code>hash</code>) VALUES ( '$name', '$email', '$password', '$hash')&quot;;
$created_user = mysqli_query($con,$query_create_user);
if (!$created_user) {
echo 'Query Failed ';

if (mysqli_affected_rows($con) == 1) { //If the Insert Query was successfull.

$subject = 'Activate Your Email';

$headers = &quot;From: \r\n&quot;;
$headers .= &quot;MIME-Version: 1.0\r\n&quot;;
$headers .= &quot;Content-Type: text/html; charset=ISO-8859-1\r\n&quot;;
$url= BASE_PATH . '/verify.php?email=' . urlencode($email) . &quot;&amp;key=$hash&quot;;

$message ='&lt;p&gt;To activate your account please click on Activate buttton&lt;/p&gt;';
$message.='&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;&gt; &lt;tr&gt;';
$message .= '&lt;td align=&quot;center&quot; width=&quot;300&quot; height=&quot;40&quot; bgcolor=&quot;#000091&quot; style=&quot;-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px;

color: #ffffff; display: block;&quot;&gt;';

$message .= '&lt;a href=&quot;'.$url.'&quot; style=&quot;color: #ffffff; font-size:16px; font-weight: bold; font-family: Helvetica, Arial, sans-serif; text-decoration: none;

line-height:40px; width:100%; display:inline-block&quot;&gt;Click to Activate&lt;/a&gt;';
$message .= '&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;';

mail($email, $subject, $message, $headers);

echo '&lt;div&gt;A confirmation email
has been sent to &lt;b&gt;'. $email.' &lt;/b&gt; Please click on the Activate Button to Activate your account &lt;/div&gt;';
} else { // If it did not run OK.
echo '&lt;div&gt;You could not be registered due to a system
error. We apologize for any
echo '&lt;div&gt;Email already registered&lt;/div&gt;';}


Code For Verfify.php:

$email = trim(mysql_escape_string($_GET['email']));

$key = trim(mysql_escape_string($_GET['key']));

$query_verify_email = "SELECT * FROM register WHERE email ='$email' and isactive = 1";
$result_verify_email = mysqli_query($con,$query_verify_email);

if (mysqli_num_rows($result_verify_email) == 1)
echo '<div>Your Account already exists. Please <a href="login.html">Login Here</a></div>';


if (isset($email) && isset($key))


mysqli_query($con, "UPDATE register SET isactive=1 WHERE email ='".$email."' AND hash='".$key."' ") or die(mysql_error());

if (mysqli_affected_rows($con) == 1)
echo '<div>Your Account has been activated. Please <a href="login.html">Login Here</a></div>';

} else
echo '<div>Account couldnot be activated.</div>';



You can Download the file and try it on your server.
NOTE: This will not work in localserver because localserver doesn’t have mail server configured by default. So if you want to test it in localserver then I would suggest you to use any email server package like PHPMailer or sendmail. Here’s an article explaining how to send email from local server.

Share this tutorial with your friends and followers :)

About sanjeev

Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

48 Responses so far.

  1. sir,
    I would like, how to create a list of mails,and at time to sending information(ex: temple design or plain-text) receive that all mails.

    How to create a mailchimp campaigns. please kindly i request to you post the information to that link.

        1. thanks, i already can access the file but i get another problem when login system, its always say: Incorrect Credentials, You need to register

          how can i solve that problem, please help me :(

        2. Hello InfoTuts, same problem above. my email is already registered, but i can’t download your code.

          any help from you?
          Thanks in Advance

  2. thanks it very nice. but i want added code for after we click the activation verification link it automatically verified and redirect the user dasboard or user main page not again login page.. please help me thanks in advance

  3. Hello Sir I want to download this feature for my site. But this feature does not download. Plz tell me how can i download this feature.

  4. Your “Email Verification on User Registration In PHP” cannot be downloaded after subscribe. Same problems as others. How to download? thanks

  5. dr sir, i have already subscribe on this website but i still cant download the source file.?? can you plz send me the file..thanks in advance

      1. Parse error: syntax error, unexpected ‘;’ in C:\xampp\htdocs\email-verification-php\register.php on line 11. What’s up with that? I added and deleted the bracket but still i got that error.

  6. Dear Sir
    i’m already subcribed but there are some problem for downloading..
    pls help me..
    I want to learn, Is it working?

  7. Parse error: syntax error, unexpected ‘;’ in C:\wamp\www\email-verification-php\register.php on line 11

    how to fix this?

  8. Hi,
    Im using your code to send email and its working perfectly. The issue I have is that Im using MVC Framework. How do I set the URL using the email and key to verify the user (

Leave a Reply

Your email address will not be published. Required fields are marked *