Today we will learn how to verify email id while signup. it's very important for the real user. You must have any user verification system while signup. Otherwise you cannot prevent illegal activities of users. you can choose email verification or phone verification.
Here we will use php mailer to varify email ID.
Here step-by-step i tried to explain.
Create table , verification code generate , get user IP
<?php
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`user_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`pass` varchar(50) NOT NULL,
`is_active` int(11) NOT NULL DEFAULT '0',
`varify_code` varchar(255) NOT NULL,
`varify_validity` int(25) NOT NULL,
`get_ip` varchar(255) NOT NULL,
`get_browser` varchar(255) NOT NULL,
`create_time` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `user`
ADD PRIMARY KEY (`id`);
ALTER TABLE `user`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
?>
Step 2 Connect db (PDO Or mysqli) I am using PDO connect.php
<?php
class drdatabase{
private static $dbname='dbname';
private static $dbhost='localhost';
private static $dbusername='user';
private static $dbuserpassword='pass';
private static $cont= null;
public function __construct(){
die('init function not access');
}
public static function connect(){
if(null== self::$cont)
{
try
{
self::$cont= new PDO("mysql:host=".self::$dbhost.";" ."dbname=".self::$dbname,
self::$dbusername,self::$dbuserpassword);
}
catch (PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect()
{
self::$cont=null;
}
function msg2()
{
echo "thanks";
}
}
?>
Step 3 functions.php
<?php
function get_client_ip()
{
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
function val_code()
{
$length = 8;
$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXY";
$Gcode = "";
for ($p = 0; $p < $length; $p++)
{
$Gcode.= $characters[rand(0, strlen($characters))];
}
return $Gcode;
}
?>
Step 5. signup.php
<?php
include_once('connect.php');
$db=drdatabase::connect();
include_once('functions.php');
$mmsg="";
if(isset($_POST['Username'])){
$ips=md5(get_client_ip());
$browser=md5($_SERVER['HTTP_USER_AGENT']);
$joindate=mktime();
$valid_time=strtotime('+1 day', $joindate);
$code=val_code();
$username=$_POST['Username'];
$email=$_POST['email'];
$Password=md5($_POST['Password']);
$varify_link='<a href="https://www.yourdomin.in/active.php?log='.$ips.'&vad='.$browser.'"> Varify Link Here</a> ';
$sql_ck_user="select 1 from user where email='$email'";
$data_ck_user=$db->prepare($sql_ck_user);
$data_ck_user->execute();
$res_ck=$data_ck_user->fetch();
if($res_ck)
{
$mmsg='<span style="color:red">User Already Exists</span>';
}
else{
$joindate=mktime();
$query ="insert into user (user_name,
email,
pass,
is_active,
varify_code,
varify_validity,get_ip,get_browser,create_time)
values('$username',
'$email',
'$Password',
0,
'$code',$valid_time,
'$ips','$browser',$joindate)";
$data_add=$db->prepare($query);
$result=$data_add->execute();
if($result)
{
include_once('PHPMailer/PHPMailerAutoload.php');
$mmmss="Thank You For Join With Us!";
$subject = "Online Exam ";
$msg='Hi.<br>'.$username.'<br>'.$mmmss.'<br> Varification Code :'.$code.'<br> Varify Link :'.$varify_link;
$mail = new PHPMailer;
$mail->setFrom("info@yourdomin.in", "yourdomin.in");
$mail->addAddress("$email", "$username");
$mail->IsHTML(true);
$mail->Subject = $subject."!";
$mail->Body = $msg;
if(!$mail->send()) {
$eMSG= 'Message was not sent.';
echo 'Mailer error: ' . $mail->Errorsubscribe;
}
else
{
$mmsg='<span style="color:green">Varification Pending. Please check your mail </span>';
}
}
}
}
?>
<form name="form1" method="POST" class="w3-container" style="margin-top:20px">
<label>User Name :</label>
<input class="w3-input" autocomplete="off" id="Username" name="Username" type="text">
<label>Email :</label>
<input class="w3-input" autocomplete="off" id="email" onkeyup="" name="email" type="text">
<span id="result"></span>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
<label>Password :</label>
<input class="w3-input" type="password" id="Password" name="Password" placeholder="Password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
<button type="submit" name="signup" onclick="" class="w3-btn w3-padding w3-blue" style="width:150px">Signup ?</button>
<a href="login.php" name="signupT" onclick="" class="w3-btn w3-padding w3-blue" style="width:150px">Login ?</a>
</p>
</form>
Step 5.verify.php
During verification we check the users Ip and browser. you just have to do one thing, Check the login time,user verified or not. if not ,redriect to verify page.
<?php
include_once('connect.php');
$db=drdatabase::connect();
$msg="";
$process=false;
include_once('functions.php');
if(isset($_GET['log']))
{
$get_log=$_GET['log'];
$get_vad=$_GET['vad'];
$gettime=mktime();
$ips=get_client_ip();
$ips2=md5($ips);
$browser=$_SERVER['HTTP_USER_AGENT'];
$browser2=md5($browser);
if($get_log==$ips2 && $get_vad==$browser2)
{
$process=true;
}else{
header('Location:index.php');
}
}else{
header('Location:index.php');
}
if(isset($_POST['email']))
{
$email=$_POST['email'];
$codes=$_POST['codes'];
$chcode=val_code();
if($email!=""||$codes!="")
{
$sql_ck="select 1 from user where email='$email' and get_ip='$ips' and get_browser='$browser' and varify_validity>$gettime";
$data_ck=$db->prepare($sql_ck);
$data_ck->execute();
$res=$data_ck->fetch();
if($res)
{
$sql="update user set is_active=1,varify_code='$chcode' where email='$email' and varify_code='$codes' and get_ip='$ips' and get_browser='$browser' and varify_validity>$gettime";
echo $sql;
$data=$db->prepare($sql);
$res2=$data->execute();
if($res2)
{
header('Location:login.php');
}
}else{
$msg="Not Valid";
}
}
}
echo $msg;
if($process==true)
{
?>
<form name="form1" method="POST" class="w3-container" style="margin-top:20px">
<label>Email :</label>
<input class="w3-input" autocomplete="off" id="email" onkeyup="" name="email" type="text">
<label>Code Here :</label>
<input class="w3-input" type="password" id="codes" name="codes" placeholder="Password">
<button type="submit" name="signup" onclick="" class="w3-btn w3-padding w3-blue" style="width:150px">Signup ?</button>
</p>
</form>
<?php
}
?>
Today we will learn how to verify email id while signup. it's very important for the real user. You ..
This is a common problem for those of you who are new to coding. While coding we often make some syn..
Warning: session start() [function.session-start]: Cannot send session cache limiter - headers alrea..
Before we use the PDO, we will know why we will use PDO.PDO Only supports object-oriented.It has man..
Deprecated: autoload() is deprecated, use spl autoload register() instead. How to replace this (my a..
getimagesize() is a PHP function that returns an array containing information about the size and typ..
Today we will know - How to login with otp in php. Here we will use php,mysql & ajax. Before tha..
Get the latest news and updates by signing up to our daily newsletter.We won't sell your email or spam you !