I received lots tutorial requests from my readers in that most of them asked to me, how to implement email verification system using PHP. This is very basic tutorial explained how to create database and proper activation code. Implemented with mysqli_() fuctions, because mysql_() functions are depreciated.
Download Script Live Demo
Database
Sample database users table contains four columns uid, email, password, activation and status.
CREATE TABLE IF NOT EXISTS `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(300) NOT NULL UNIQUE,
`password` varchar(300) NOT NULL,
`activation` varchar(300) NOT NULL UNIQUE,
`status` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`uid`)
)
`uid` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(300) NOT NULL UNIQUE,
`password` varchar(300) NOT NULL,
`activation` varchar(300) NOT NULL UNIQUE,
`status` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`uid`)
)
HTML Code
Contains simple HTMl code.
<form action="" method="post">
<label>Email</label>
<input type="text" name="email" class="input" autocomplete="off"/>
<label>Password </label>
<input type="password" name="password" class="input" autocomplete="off"/><br/>
<input type="submit" class="button" value="Registration" />
<span class='msg'><?php echo $msg; ?></span>
</form>
<label>Email</label>
<input type="text" name="email" class="input" autocomplete="off"/>
<label>Password </label>
<input type="password" name="password" class="input" autocomplete="off"/><br/>
<input type="submit" class="button" value="Registration" />
<span class='msg'><?php echo $msg; ?></span>
</form>
db.php
Database configuration file, modify username, password, database and base url values.
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$connection = @mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$base_url='http://www.youwebsite.com/email_activation/';
?>
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$connection = @mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$base_url='http://www.youwebsite.com/email_activation/';
?>
index.php
Contains PHP code, storing user registration values into users table. Here activation code generation using MD5 encryption.
<?php
include 'db.php';
$msg='';
if(!empty($_POST['email']) && isset($_POST['email']) && !empty($_POST['password']) && isset($_POST['password']) )
{
// username and password sent from form
$email=mysqli_real_escape_string($connection,$_POST['email']);
$password=mysqli_real_escape_string($connection,$_POST['password']);
// regular expression for email check
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
if(preg_match($regex, $email))
{
$password=md5($password); // encrypted password
$activation=md5($email.time()); // encrypted email+timestamp
$count=mysqli_query($connection,"SELECT uid FROM users WHERE email='$email'");
// email check
if(mysqli_num_rows($count) < 1)
{
mysqli_query($connection,"INSERT INTO users(email,password,activation) VALUES('$email','$password','$activation')");
// sending email
include 'smtp/Send_Mail.php';
$to=$email;
$subject="Email verification";
$body='Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> <a href="'.$base_url.'activation/'.$activation.'">'.$base_url.'activation/'.$activation.'</a>';
Send_Mail($to,$subject,$body);
$msg= "Registration successful, please activate email.";
}
else
{
$msg= 'The email is already taken, please try new.';
}
}
else
{
$msg = 'The email you have entered is invalid, please try again.';
}
}
// HTML Part
?>
include 'db.php';
$msg='';
if(!empty($_POST['email']) && isset($_POST['email']) && !empty($_POST['password']) && isset($_POST['password']) )
{
// username and password sent from form
$email=mysqli_real_escape_string($connection,$_POST['email']);
$password=mysqli_real_escape_string($connection,$_POST['password']);
// regular expression for email check
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
if(preg_match($regex, $email))
{
$password=md5($password); // encrypted password
$activation=md5($email.time()); // encrypted email+timestamp
$count=mysqli_query($connection,"SELECT uid FROM users WHERE email='$email'");
// email check
if(mysqli_num_rows($count) < 1)
{
mysqli_query($connection,"INSERT INTO users(email,password,activation) VALUES('$email','$password','$activation')");
// sending email
include 'smtp/Send_Mail.php';
$to=$email;
$subject="Email verification";
$body='Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> <a href="'.$base_url.'activation/'.$activation.'">'.$base_url.'activation/'.$activation.'</a>';
Send_Mail($to,$subject,$body);
$msg= "Registration successful, please activate email.";
}
else
{
$msg= 'The email is already taken, please try new.';
}
}
else
{
$msg = 'The email you have entered is invalid, please try again.';
}
}
// HTML Part
?>
Send_Mail.php
Sending email function, just modify SMTP host, username and password. Here you can use GMail SMTP details for testing click here to see GMail SMTP article.
<?php
function Send_Mail($to,$subject,$body)
{
require 'class.phpmailer.php';
$from = "[email protected]";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.yourwebsite.com"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "SMTP_Username"; // SMTP username
$mail->Password = "SMTP_Password"; // SMTP password
$mail->SetFrom($from, 'From Name');
$mail->AddReplyTo($from,'From Name');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
function Send_Mail($to,$subject,$body)
{
require 'class.phpmailer.php';
$from = "[email protected]";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.yourwebsite.com"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "SMTP_Username"; // SMTP username
$mail->Password = "SMTP_Password"; // SMTP password
$mail->SetFrom($from, 'From Name');
$mail->AddReplyTo($from,'From Name');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
activation.php
Contains PHP code, here based on activations code user status updating from 0 to 1.
<?php
include 'db.php';
$msg='';
if(!empty($_GET['code']) && isset($_GET['code']))
{
$code=mysqli_real_escape_string($connection,$_GET['code']);
$c=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code'");
if(mysqli_num_rows($c) > 0)
{
$count=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code' and status='0'");
if(mysqli_num_rows($count) == 1)
{
mysqli_query($connection,"UPDATE users SET status='1' WHERE activation='$code'");
$msg="Your account is activated";
}
else
{
$msg ="Your account is already active, no need to activate again";
}
}
else
{
$msg ="Wrong activation code.";
}
}
?>
//HTML Part
<?php echo $msg; ?>
include 'db.php';
$msg='';
if(!empty($_GET['code']) && isset($_GET['code']))
{
$code=mysqli_real_escape_string($connection,$_GET['code']);
$c=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code'");
if(mysqli_num_rows($c) > 0)
{
$count=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code' and status='0'");
if(mysqli_num_rows($count) == 1)
{
mysqli_query($connection,"UPDATE users SET status='1' WHERE activation='$code'");
$msg="Your account is activated";
}
else
{
$msg ="Your account is already active, no need to activate again";
}
}
else
{
$msg ="Wrong activation code.";
}
}
?>
//HTML Part
<?php echo $msg; ?>
Email Verification
.htaccess
URL redirection script it turns
http://website.com/activation.php?code=ACTIVATION_CODE
to
http://website.com/activation/ACTIVATION_CODE
RewriteEngine On
RewriteRule ^activation/([a-zA-Z0-9_-]+)$ activation.php?code=$1
RewriteRule ^activation/([a-zA-Z0-9_-]+)/$ activation.php?code=$1
RewriteRule ^activation/([a-zA-Z0-9_-]+)$ activation.php?code=$1
RewriteRule ^activation/([a-zA-Z0-9_-]+)/$ activation.php?code=$1
CSS code
body
{
font-family: "Helvetica",Arial,sans-serif;
font-weight: 500;
color:#333;
}
label
{
width:100px;
display:block;
font-weight:bold;
color:#666666;
}
#main
{
margin:0 auto;
width:800px;
}
.input
{
padding:10px;
font-size:14px;
border:1px solid #999999;
width:200px;
margin-bottom:10px;
}
.button {
padding:10px;
background-color: #5fcf80 !important;
border-color: #3ac162 !important;
}
.msg
{
font-size:11px;
color:#666;
padding:10px;
}
{
font-family: "Helvetica",Arial,sans-serif;
font-weight: 500;
color:#333;
}
label
{
width:100px;
display:block;
font-weight:bold;
color:#666666;
}
#main
{
margin:0 auto;
width:800px;
}
.input
{
padding:10px;
font-size:14px;
border:1px solid #999999;
width:200px;
margin-bottom:10px;
}
.button {
padding:10px;
background-color: #5fcf80 !important;
border-color: #3ac162 !important;
}
.msg
{
font-size:11px;
color:#666;
padding:10px;
}
good job!!
ReplyDeletegreat... Thanks
Deletethanks lot
Deletesuprb (y)
ReplyDeletef-yah ive been waiting for this.
ReplyDeletethank you so much..canyou please make one for admin area? where admin can de-activate the users account?
That's a nice tutorial, thanks for the email.
ReplyDeleteWhy use you the google comment system?
ReplyDeleteThis afful!
Otherwise, thanks for the tutorial, but how can I implemet to wordpress?
nice tutorial.. I was creating similar tutorial and just saw you published. I liked that url redirection part.
ReplyDeleteThanks
ReplyDeleteAmazing script
ReplyDeleteu r so great..thanks alote..
ReplyDeleteGreat Article !!!
ReplyDeleteSimple And Nice Tutorial. Looking for more.
ReplyDeletethanks
ReplyDeletesir is this working on wamp server?
ReplyDeletewhy use URL redirection script it turns
ReplyDeletenicely implemented... :)
ReplyDeletehello
ReplyDeletehey ! Yes it's useful
it can be use to validate classifieds ads or commenting system
thanks
Hi there,
ReplyDeleteIf mysql_() functions are deprictaed, is it good to use mysql_real_escape_string() ? Is there another function which is better than mysql_real_escape_string() to escape quoted values?
nyc one thanks
ReplyDeleteM getting this error
ReplyDeleteWarning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'feelcomf'@'localhost' (using password: NO) in /home/feelcomf/public_html/sadhvigroups.com/demo/emailconfirmation/index.php on line 8
great script
ReplyDeleteHi, nice script! But why did you use "mysql_real_escape_string" and there a mysqli function for?
ReplyDeleteonly srinivas tamada can do like this!! awesome hatsoff @srinivastamada
ReplyDeleteBasic step in user registration system came so late for an advanced site..weird!...Useful for a newbie though
ReplyDeletePerfect
ReplyDeleteGud one..Love the post by you..
ReplyDeleteHi friend.,
ReplyDeleteHere my small suggestion is, have a small bug in that kindly change if you are going to use this code
in INDEX.PHP
// email check
if(mysqli_num_rows($count) < 1)// the number of rows should be == 1 or >0
{
mysqli_query($connection,"INSERT INTO users(email,password,activation) VALUES('$email','$password','$activation')");
// sending email
Hi. its a great stuff but there are two things that can be done.
ReplyDeleteon latest version of PHP we can use
var_dump(filter_var('[email protected]', FILTER_VALIDATE_EMAIL)); instead of regular expression
also and additional check to see if the user is already registered or have registered but the verification is pending and on the basis of that we can send mail.
Keep rocking.
www,thesoftwareguy.in
really great tutorial.
ReplyDeleteThanks a lot
ReplyDeleteGreat Script
ReplyDeleteThanks...
ReplyDeleteThanks for the detailed script
ReplyDeleteSuperb Script
ReplyDeletethanks
ReplyDeleteyou are great and thanks a lot for such posts
ReplyDeletetoday i sent mail from local_host using gmail_smtp on window platform .
Good Post. I helped me alot.
ReplyDeleteNice Site ...very useful Tutorial....
ReplyDeleteAwesome
ReplyDeleteThanks Srinivas, I was in search of this code. Your tutorials are very useful and very easy to implement.
ReplyDeleteThanks again for such beautiful code of email verification.
Great and helpful article
ReplyDeleteI'm a big fan of your blog... keep the tutorials coming! thanks.
ReplyDeletegr8 work
ReplyDeleteGreat article…I have read many topics here all are very good…
ReplyDeletenice
ReplyDeleteNice article.
ReplyDeleteneed help 'http://www.youwebsite.com/email_activation/'; want to know where is email_activation.php file new to developing bare with me thanks
ReplyDeleteNice article.
ReplyDeletecan anyone tell me what is "class.phpmailer.php" and where can i found it???????
ReplyDeleteSUDDENLY MY SMTP EMAIL VERIFCATION STOPED SENDING EMAILS,,,,, NO RESPONSE
ReplyDeleteawsome site.. very helpful..
ReplyDeletewhat to change in send.php to make it working,,
ReplyDeleteHow about keeping activation code in users sessions?
ReplyDeleteWarning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\email_activation\index.php on line 17
ReplyDeleteWarning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\email_activation\index.php on line 18
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\email_activation\index.php on line 20
plz help to resolve these warnings
this tutorial helps me a lot ..
ReplyDeletethanks bro ...
u r my teacher ...
Hi can you please help me:
ReplyDeletewhere should i write this code, I mean in which file, the code:
RewriteEngine On
RewriteRule ^activation/([a-zA-Z0-9_-]+)$ activation.php?code=$1
RewriteRule ^activation/([a-zA-Z0-9_-]+)/$ activation.php?code=$1
plz reply me. waiting for your reply.............
[email protected]
let me know when u reply On [email protected]
Hi,
ReplyDeleteCan anyone help me with how to resolve this error?
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Backstretch\emailauthIndex.php on line 24
Also, the code works really well, except data from the form does not seem to be getting to my database and I have no idea why...everything else works okay, email with activation code is sent but I can keep signing up with the same email address and the system won't know.
Please help!
Thanks.
Hi everyone,
ReplyDeletethe script is written very well manner, easy to understand. I am getting problems during the execution of this script.
1) When i am clicking on activation link to activate an account, it's showing that URL /activation/74bedce29eab40e1572bda5beb922386 was not found on this server.
2) I am not getting the $_GET['code'], from where the input variable code came, in activation.php file. if its from activation link, please guide me how to get activation link to activation.php files.
email-id: [email protected]
Thanks in advance
Hi,
ReplyDeleteafter clicking on activation link i am getting this error:
****************
Not Found
****************
The requested URL /emailsmtp/includes/f727ac95140514b002f625885f63b258 was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
********************************************************************************************
.htaccess file is placed at same location with activation.php and index.php files:
********************************************************************************************
RewriteEngine On
RewriteRule ^emailsmtp/includes/([a-zA-Z0-9_-]+)$ activation.php?code=$1
RewriteRule ^emailsmtp/includes/([a-zA-Z0-9_-]+)/$ activation.php?code=$1
please guide me.
email-id: [email protected]
Hi,
ReplyDeleteI used this code to send email to the clients. The HTML tags is working in Gmail but not in Yahoo and other Mail Servers.
Please help.
Thanks.
Error: Parse error: syntax error, unexpected end of file in /home/XXXXXXXXXX/public_html/gmail/smtp/class.phpmailer.php on line 811
ReplyDeletewhere is class.phpmailer.php file ????????
ReplyDeletehi sir
ReplyDeletehow can i create in html send verification mail for activation of ad that i want to post or submit in my site.
i want to ad category place ad and when someone submit ad then a verification mail should go to his email and then click verification mail after ad active
please help me
waiting for your reply
thank you :)
ReplyDeletevery nice article.....
ReplyDeletethat email can working in offline mode or must online mode?thank you.
ReplyDeleteMySQL has not been deprecated. It is being thought if it has to be or not.
ReplyDeleteis it work in localhost???
ReplyDeleteit is giving me error to inport db
in mylocalhost
tell its solution
it will not work on localhost you have to upload your content on server.. and admin nyc tutorial but can you please make tutorial for getting forgot password retrieval with email...?
ReplyDeletenice work! could you tell me how to integrate it with prestashop1.6
ReplyDeleteAll the topic posted here is so useful for freshers like me . Thanks
ReplyDeleteI have no idea how to trigger the form, i'm using an external index.html, even your action="", how do they really trigger??
ReplyDeletemail was not sent only insert into data base please help
ReplyDeleteYour post is really helpful for me. Thanks for your wonderful post. It is really very helpful for us and I have gathered some important information from this blog.
ReplyDeletegooooooooodddddddddd jobbbbbbbbbbbbbbbb
ReplyDeletemail was not sent only insert into data base please help.
ReplyDeleteFantastic.Thanks for helpe.
ReplyDeleteThat's very useful for me..but i need one help..how to check the email id is already exists or not using php please help me..
ReplyDeletewhere to put the code of activation link and to create activation link.
ReplyDeleteRewriteEngine On
RewriteRule ^activation/([a-zA-Z0-9_-]+)$ activation.php?code=$1
RewriteRule ^activation/([a-zA-Z0-9_-]+)/$ activation.php?code=$1
email verification/activation link not going to mail.how to send link.
ReplyDeleteI m looking for something like that
ReplyDeletea mail drop to his mail and user verify this email by cliking on this...
Note: Little problem with my SMTP server, emails are not working. I am working on this issue.
ReplyDeletei got Registration successful, please activate email. but mail did not receive..
ReplyDeleteany problem??? plz help me brother!!!
Your code doesnt work.. You should use send_mail (); to call the function in index.php
ReplyDeletehi,may i know how to fix these warnings..
ReplyDeleteWarning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\email_activation\index.php on line 7
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\email_activation\index.php on line 8
after click on link in mail its showing page not found
ReplyDeleteHi i was wondering if this script can be used for mssql. Do i need to replace mssqli?newbie thanks
ReplyDeleteYes you can.
DeleteI am not recieving any verification and your live demo is also not working.
ReplyDeletenice it is very help full to us
ReplyDeleteI CANT :(
ReplyDeleteWarning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\a14\email_activation2\index.php on line 7
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\a14\email_activation2\index.php on line 8
what must I do?
Include connection mysqli_real_escape_string($connection, $value);
DeleteThis will not work for me can you please create a code for woocommerce enquiry form in which user enter their email address and it will first verificate and the send email to admin for order.
ReplyDeleteFacing this error !!!
ReplyDeleteParse error: syntax error, unexpected '$fname' (T_VARIABLE)
hi
ReplyDeleteI am getting this error
PHP Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli
Please help me with this
can u please tell me .htaccess file and do i need to type .htaccess code or is it builtin??
ReplyDeleteThe requested URL /final_home/mailvarify/email_activation/activation/f271e02fcf28a159c9dead8a622fa358 was not found on this server.
ReplyDeleteAdditionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request
this error is coming please resolve.
thanks in advance
Thank you
ReplyDeleteGood one
ReplyDeleteNot getting activation mail, tired with gmail and my personal server.what could be the reason for this? Please help me to find out this
ReplyDeleteThis post was quite awesome and interesting to read. Congrats for your work. Thanks a lot for providing this with us. Keep on updating this with us regularly.
ReplyDeleteHi, i am getting this error
ReplyDeletePlease somebody help
Fatal error: Uncaught Error: Class 'SMTP' not found in C:\xampp\htdocs\Awwranze\class.phpmailer.php:1520 Stack trace: #0 C:\xampp\htdocs\Awwranze\class.phpmailer.php(1603): PHPMailer->getSMTPInstance() #1 C:\xampp\htdocs\Awwranze\class.phpmailer.php(1540): PHPMailer->smtpConnect(Array) #2 C:\xampp\htdocs\Awwranze\class.phpmailer.php(1335): PHPMailer->smtpSend('Date: Fri, 5 Ma...', 'This is a multi...') #3 C:\xampp\htdocs\Awwranze\class.phpmailer.php(1213): PHPMailer->postSend() #4 C:\xampp\htdocs\Awwranze\Send_Mail.php(20): PHPMailer->send() #5 C:\xampp\htdocs\Awwranze\register.php(147): Send_Mail('sonalkakkar7@gm...', 'Email verificat...', 'Hi,
...') #6 {main} thrown in C:\xampp\htdocs\Awwranze\class.phpmailer.php on line 1520
Fatal error: Uncaught Error: Call to undefined function mysql_real_escape_string() in /opt/lampp/htdocs/demos/email_activation/index.php:7 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/demos/email_activation/index.php on line 7
ReplyDeletehttps://demos.9lessons.info/email_activation/index.php
ReplyDeleteDemo has been fixed. Please try to replace mysql_ with mysqli_ and follow the article code.
Deletemysql_ methods has been depreciated.
Please create a tutorial video explaining the same. Thank You.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete