DBA > Articles

PHP Email Verification Script Using MySQLi

To read more DBA articles, visit http://dba.fyicenter.com/article/

Most of the websites have feature to verify your email upon user registration to avoid spamming and fake registration. The easiest and common way to verify email id using PHP and MySQLi is to send an activation link on registered email id and user will be activated once click on activation link.

Db Connection:
define('_HOST_NAME', 'localhost');
define('_DATABASE_USER_NAME', 'root');
define('_DATABASE_PASSWORD', '');
define('_DATABASE_NAME', 'demo');
$dbConnection = new mysqli(_HOST_NAME, _DATABASE_USER_NAME,
if ($dbConnection->connect_error) {
trigger_error('Connection Failed: ' . $dbConnection->connect_error, E_USER_ERROR);

index.php Get the source code for How to send SMTP email using PHP. If you are using php version 5.5 or more then use PHP5.5 secure hashing for password

require_once 'db_connection.php';
$user_email = trim($dbConnection->real_escape_string($_POST['user_email']));
$user_password = $dbConnection->real_escape_string($_POST['user_password']);
$regular_expression = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';

if(preg_match($regular_expression, $user_email)){
$checkEmailId = "SELECT id FROM tbl_users_registration WHERE email_id = '".$user_email."'";
$rows_returned = $resEmail->num_rows;
if($rows_returned <0){
$msg = 'The email is already taken, please try new.';
$activation_key =md5($user_email.time());
$salt = 'randomstring'; //generate random string
$hashed_value = md5($salt.$user_password);
$sqlInsertUser = $dbConnection->query("INSERT INTO tbl_users_registration (email_id, password,activation_key) VALUES('$user_email', '$hashed_value','$activation_key')");
/*** Script for send email start here ***/
$subject = 'StepBlogging :: Email verification';
$body='Please verify your email <br/> <br/> <a href="www.stepblogging.com/activate_account.php?key='.$activation_key.'">www.stepblogging.com/activate_account.php?key='.$activation_key.'';
include 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true;
$mail->Username = _SMTP_USER_NAME; //SMTP Username
$mail->Password = _SMTP_PASSWORD; //SMTP Password
$mail->Host = _SMTP_HOST_NAME; //SMTP Hostname
$mail->Port = _SMTP_PORT; //SMTP Port
$mail->LE = "\r\n";

$mail->setFrom('from_email@yourdomain.com', 'Your Name');
$mail->AddAddress($user_email); // name is optional
$mail->Subject = $subject;
$mail->Body = $body;
/*** Script for send email end here ***/
$msg = 'Registration successful, please activate email.';
$msg = 'The email you have entered is invalid, please try again.';
<title>PHP Email Verification Script Using PDO</title>
<div class='web'>
<form class="myform" action="index.php" method="post">
<?php if(isset($msg)) echo $msg; ?>
<div class='input'>Email: <div><input type="text" name="user_email"/></div>
<div class='input'>Password: <input type="password" name="user_password" /></div>
<input type="submit" value="register" name="register" Class="btn" />

Full article...

Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/