Send Verification link on email after registration in PHP
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/
Hi all , Welcome to shubhammaurya.com , Today we are going to discuss ,
How to Send Verification link on email after registration in PHP
LET START
So, make a database and paste the below code OR make your own.
-- 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 */;
So, To start first make a file in notepad and save it as dbconfig.php and paste the below code.
<?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); ?>
Second Step 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(); $mail->SMTPDebug = 0; $mail->SMTPAuth= true; $mail->SMTPSecure="ssl"; $mail->Host="smtp.gmail.com"; $mail->Port=465; $mail->AddAddress($email); $mail->Username="Your Email id"; $mail->Password="Your Password"; $mail->SetFrom('Your Email id'); $mail->Subject = $subject; $mail->MsgHTML($message); $mail->Send(); return true; } //Get Last Inserted Id public function lastID() { $stmt = $this->db->lastInsertId(); return $stmt; } } ?>
Third Step 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>
Fouth Step 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 : <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 : <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 : <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>
Find Code at github : click here
Download Code From Here : click here
Comment Below, If any problem occurs.
STAY CONNECTED FOR MORE
Your images are all blank because the source urls are wrong e.g.
https://shubhammaurya.com/public_html/shubhammaurya.com/wp-content/uploads/2017/08/Capture-6-760×274.png
at the top of this post, instead of:
https://shubhammaurya.com/wp-content/uploads/2017/08/Capture-6-760×274.png
Thanks for the phpMailer fix.
That is because i changed the Server.
I was able to find good information from your content.
Hello, i feel that i saw you visited my website so i
came to go back the prefer?.I am trying to in finding things to enhance
my site!I suppose its ok to use a few of your concepts!!
Paragraph writing is also a fun, if you know then you can write otherwise it
is difficult to write.
Good day! Do you know if they make any plugins to help with SEO?
I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success.
If you know of any please share. Thank you!
We are a group of volunteers and starting a new scheme in our community.
Your website provided us with valuable info to work on. You’ve done
an impressive job and our entire community will be thankful to you.
Your Welcome
Nice post. I learn something totally new and challenging on websites I stumbleupon on a daily basis.
It will always be useful to read through content from other authors and use something from their web sites.
Good day! I simply wish to offer you a huge thumbs up for the
excellent information you have here on this post.
I’ll be coming back to your web site for more soon.
I do not even know how I ended up here, but I thought this post was great.
I do not know who you are but certainly you’re going to
a famous blogger if you aren’t already 😉 Cheers!
For newest information you have to go to see the web and on web I found this web site as a best website for latest updates.
Great article. I am dealing with a few of these issues as well..
With havin so much content do you ever run into any
problems of plagorism or copyright infringement? My blog
has a lot of exclusive content I’ve either authored myself
or outsourced but it looks like a lot of it is popping it up all over the internet without my authorization. Do you know any ways to help stop content from being stolen? I’d certainly appreciate it.
Right away I am going to do my breakfast, when having my breakfast coming over again to read further
news.
Excellent write-up. I absolutely appreciate this website. Keep writing!
Sweet blog! I found it while searching on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Thank you
Great post.
I’ve been surfing on-line greater than 3
hours nowadays, but I by no means discovered any attention-grabbing article like yours.
It is beautiful worth enough for me. In my view, if
all webmasters and bloggers made good content as you probably did, the web might be a
lot more useful than ever before.
An impreѕsive share! I’ve just forwarded this onto a colleague who
was conducting a little һomework on this. And he in fact ordered me
breakfast due to the fact that I stumbled upon it
for him… lol. So let mе reword this….
Thank YОU for the meal!! But yeah, thanx for spending some time to talk about this matter hеre on your internet site.
Excellent article. I am facing some of these issues as well..
I know this web site offers quality depending articles or reviews and other information, is there any other
site which gives these kinds of stuff in quality?
This blog was… how do I say it? Relevant!! Finally I’ve found something which
helped me. Kudos!
This paragraph is genuinely a fastidious one it assists new net people, who are wishing for blogging.
Exclusivey a llimited organization of humans are actually related
to the authors that are bing in the middle of speculating the games .
Accompanying our team’s Soccer Scoores anybody’s ideas could become accurate .
Exclusively a limited organization of citizens are absolutely linked
to the sources who are being in the inside of manipulating the games
!!!
Witth our crew’s soccer predictions for this week your ideas would occur valid !
This blog was… how do I say it? Relevant!!
Finally I have found something which helped me. Many thanks!