Skip to content

Instantly share code, notes, and snippets.

@Mauryashubham
Last active August 24, 2017 11:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Mauryashubham/01bc92ffbdfa71b564f8a5f71c4e781e to your computer and use it in GitHub Desktop.
Send Verification link on email after registration in PHP
<?php
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/
make a file in notepad and save it as class.User.php and paste the below code.
<?php
/**
*
*/
class USER
{
private $db;
//Constructor
function __construct($DBcon)
{
$this->db=$DBcon;
}
//Signup
public function signup($email,$code)
{
try {
$stmt=$this->db->prepare("INSERT into users(email,joindate,token_Code) VALUES(:email, CURRENT_TIMESTAMP , :code )");
if($stmt->execute(array(':email'=>$email ,':code'=>$code)))
{
return $stmt;
}
} catch (PDOException $e)
{
echo $e->getMessage();
}
}
//Redirect
public function redirect($url)
{
header("Location: $url");
exit();
}
//Send Mail
function send_mail($email,$message,$subject)
{
require_once('mailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); //Remove if mail is not sending..
$mail->SMTPDebug = 0;
$mail->SMTPAuth= true;
$mail->SMTPSecure="ssl";
$mail->Host="smtp.gmail.com";
$mail->Port=465;
$mail->AddAddress($email);
$mail->Username="maurya.shubham5@gmail.com";
$mail->Password="Submitted11@";
$mail->SetFrom('maurya.shubham5@gmail.com');
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->Send();
return true;
}
//Get Last Inserted Id
public function lastID()
{
$stmt = $this->db->lastInsertId();
return $stmt;
}
}
?>
-- phpMyAdmin SQL Dump
-- version 4.6.5.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Aug 24, 2017 at 01:42 PM
-- Server version: 10.1.21-MariaDB
-- PHP Version: 5.6.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `testdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`user_id` int(11) NOT NULL,
`email` varchar(255) NOT NULL,
`token_Code` varchar(255) NOT NULL,
`userstatus` varchar(10) NOT NULL DEFAULT 'N',
`joindate` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<?php
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/
$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "testdb";
try
{
$DBcon = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// echo "Done..";
}
catch(PDOException $e)
{
echo "ERROR : ".$e->getMessage();
}
include_once 'class.User.php';
//$user=new USER($DBcon);
?>
make a file in notepad and save it as index.php and paste the below code.
<?php
//connect to database
require_once 'dbconfig.php';
//Session Start
session_start();
$reg=new USER($DBcon);
//Registration
if(isset($_POST['register']))
{
$email=$_POST['email'];
$code=md5(uniqid(rand()));
try
{
if($reg->signup($email,$code))
{
if(true)
{
$id=$reg->lastID($DBcon);
$message = "
<div style='margin: auto;background-color: #fafafa;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);'>
<div style='color: white;text-align: center;background-color: #2562c5;padding: 20px;'>CONFIRM REGISTRATION</div><br>
<div style='padding: 20px;color: #FF5722;'>Hello ,<br><span style='color: #FF5722;'>$email</span>,
<br><br>
Welcome $email<br>
To complete your registration please , just click the following link : <br/>
<br>
<a style='text-decoration: blink;background: #2562c5;color: #ffffff;padding: 10px;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);' href='http://localhost/email_Veri/verify.php?id=$id&code=$code'>Click Here to Activate Your Profile.</a>
<br><br><br>
<spa>Regards<span><br>
<span>Team Shubhammaurya.com</span>
</div>
</div>";
$subject = "Confirm Registration";
if($reg->send_mail($email,$message,$subject))
{
if(true)
{
echo "<script>alert('Check your Mail to confirm')</script>";
exit;
}
}
}
// echo "<script type='text/javascript'>alert('Registration Done..!');</script>";
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<div style="background: #00BCD4; padding: 60px;">
<form method="post" autocomplete="off">
<input type="email" name="email" placeholder="Your Email" class="form-control" required="required" ><br>
<input type="submit" name="register" value="Sign up" class="btn btn-danger" >
</form>
</div>
</div>
</div>
</div>
</body>
</html>
make a file in notepad and save it as verify.php and paste the below code.
<?php
require_once 'dbconfig.php';
if(empty($_GET['id']) && empty($_GET['code']))
{
$user->redirect('index.php');
}
if(isset($_GET['id']) && isset($_GET['code']))
{
$id = $_GET['id'];
$code = $_GET['code'];
$statusY = "Y";
$statusN = "N";
$stmt = $DBcon->prepare("SELECT user_id,userstatus FROM users WHERE user_id=:uID AND token_Code=:code LIMIT 1");
$stmt->execute(array(":uID"=>$id,":code"=>$code));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
if($row['userstatus']==$statusN)
{
$stmt = $DBcon->prepare("UPDATE users SET userstatus=:status WHERE user_id=:uID");
$stmt->bindparam(":status",$statusY);
$stmt->bindparam(":uID",$id);
$stmt->execute();
$msg = "
<div style='/* text-decoration: blink; */background: #2562c5;color: #ffffff;padding: 20px;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);text-align: -webkit-center;width: 50%;margin: auto;'><span><strong>WoW !</strong> Your Account is Now Activated : &nbsp;&nbsp;<a style='background-color: white;text-decoration: blink;padding: 5px;margin: auto;' href='../index.php'>Click here to Login</a></span><div>";
}
else
{
$msg = "
<div style='/* text-decoration: blink; */background: #2562c5;color: #ffffff;padding: 20px;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);text-align: -webkit-center;width: 50%;margin: auto;'><span><strong>Sorry !</strong> Your Account is already Activated : &nbsp;&nbsp;<a style='background-color: white;text-decoration: blink;padding: 5px;margin: auto;' href='../index.php'>Click here to Login</a></span><div>";
}
}
else
{
$msg = "
<div style='/* text-decoration: blink; */background: #2562c5;color: #ffffff;padding: 20px;box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);text-align: -webkit-center;width: 50%;margin: auto;'><span><strong>Sorry !</strong> No Account Found : &nbsp;&nbsp;<a style='background-color: white;text-decoration: blink;padding: 5px;margin: auto;' href='../index.php'>Click here to Register</a></span><div>
";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="jumbotron text-center" style="margin-top: 145px;">
<?php if(isset($msg)) { echo $msg; } ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment