PHP Email Verification Script using PDO Extension
In this tutorial, we will learn how to verify a registered email id using PHP PDO extension.
This tutorial include following files
1.Registration file(index.php)
2.Email Verification file(email_verification.php)
3.Login file(login.php)
4. Welcome file(welcome.php)
5. Logout file(logout.php)
6.Database Connection(config.php)
Database include one table(userregistration)
Structure of userregistration MySQL table
1 2 3 4 5 6 7 8 9 |
CREATE TABLE IF NOT EXISTS `userregistration` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `activationcode` varchar(255) NOT NULL, `status` int(11) NOT NULL, `postingdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; |
First, we will create user registration HTML form.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<form name="insert" action="" method="post"> <table width="100%" border="0"> <tr> <th height="62" scope="row">Name </th> <td width="71%"><input type="text" name="name" id="name" value="" class="form-control" required /></td> </tr> <tr> <th height="62" scope="row">Email id </th> <td width="71%"><input type="email" name="email" id="email" value="" class="form-control" required /></td> </tr> <tr> <th height="62" scope="row">Password </th> <td width="71%"><input type="password" name="password" id="password" value="" class="form-control" required /></td> </tr> <tr> <th height="62" scope="row"></th> <td width="71%"><input type="submit" name="submit" value="Submit" class="btn-group-sm" /> </td> </tr> </table> </form> |
PHP code for data insertion or sending verification mail. Put this code top of the index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php include_once("config.php"); if(isset($_POST['submit'])) { //Post Values $name=$_POST['name']; $email=$_POST['email']; $password=md5($_POST['password']); $status=0; $activationcode=md5($email.time()); // Creating activation code $sql="insert into userregistration(name,email,password,activationcode,status) values(:name,:email,:password,:activationcode,:status)"; $query = $dbh->prepare($sql); $query->bindParam(':name',$name,PDO::PARAM_STR); $query->bindParam(':email',$email,PDO::PARAM_STR); $query->bindParam(':password',$password,PDO::PARAM_STR); $query->bindParam(':activationcode',$activationcode,PDO::PARAM_STR); $query->bindParam(':status',$status,PDO::PARAM_STR); $query->execute(); $lastInsertId = $dbh->lastInsertId(); if($lastInsertId) { $to=$email; $msg= "Thanks for new Registration."; $subject="Email verification"; $headers .= "MIME-Version: 1.0"."\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; $headers .= 'From: PHPGurukul | Programing Blog (Demo) <info@phpgurukul.com>'."\r\n"; //Change this to your email $ms.="<html></body><div><div>Dear $name,</div></br></br>"; $ms.="<div style='padding-top:8px;'>Please click The following link For verifying and activation of your account</div> <div style='padding-top:10px;'><a href='http://yourdomain.com/email_verification.php?code=$activationcode'>Click Here</a></div> </body></html>"; mail($to,$subject,$ms,$headers); echo "<script>alert('Registration successful, please verify in the registered Email-Id');</script>"; echo "<script>window.location = 'login.php';</script>";; } else { echo "<script>alert('Something went wrong. Please try again.');</script>"; } } ?> |
Code for Email verification (email_verification.php). After clicking the verification link you will redirect to email_verification.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php include 'config.php'; if(!empty($_GET['code']) && isset($_GET['code'])) { $code=$_GET['code']; $sql = "SELECT * FROM userregistration WHERE activationcode=:code"; $query = $dbh -> prepare($sql); $query->bindParam(':code',$code,PDO::PARAM_STR); $query->execute(); $results=$query->fetchAll(PDO::FETCH_OBJ); $cnt=1; if($query->rowCount() > 0) { $st=0; $sql = "SELECT id FROM userregistration WHERE activationcode=:code and status=:st"; $query = $dbh -> prepare($sql); $query->bindParam(':code',$code,PDO::PARAM_STR); $query->bindParam(':st',$st,PDO::PARAM_STR); $query->execute(); $results=$query->fetchAll(PDO::FETCH_OBJ); $cnt=1; if($query->rowCount() > 0) { $st=1; $sql = "UPDATE userregistration SET status=:st WHERE activationcode=:code"; $query = $dbh->prepare($sql); $query -> bindParam(':code',$code, PDO::PARAM_STR); $query-> bindParam(':st',$st, PDO::PARAM_STR); $query -> execute(); } else { $msg ="Your account is already active, no need to activate again"; } } else { $msg ="Wrong activation code."; } } ?> |
After successful verification. You can Login (login.php)
login form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<form name="insert" action="" method="post"> <table width="100%" border="0"> <tr> <td colspan="2"><font color="#FF0000"><?php echo $_SESSION['action1']; ?><?php echo $_SESSION['action1']="";?></font></td> </tr> <tr> <th height="62" scope="row">Email id </th> <td width="71%"><input type="email" name="email" id="email" value="" class="form-control" required /></td> </tr> <tr> <th height="62" scope="row">Password </th> <td width="71%"><input type="password" name="password" id="password" value="" class="form-control" required /></td> </tr> <tr> <th height="62" scope="row"></th> <td width="71%"><input type="submit" name="login" value="Submit" class="btn-group-sm" /> </td> </tr> </table> </form> |
PHP Code for login. Put this code ate the top of the login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php session_start(); error_reporting(0); include_once("config.php"); if(isset($_POST['login'])) { $email=$_POST['email']; $password=md5($_POST['password']); $sql ="SELECT * FROM userregistration WHERE email=:email and password=:password"; $query= $dbh -> prepare($sql); $query-> bindParam(':email', $email, PDO::PARAM_STR); $query-> bindParam(':password', $password, PDO::PARAM_STR); $query-> execute(); $results=$query->fetchAll(PDO::FETCH_OBJ); if($query->rowCount() > 0) { //fetching name foreach ($results as $row) { $uname=$row->name; } $_SESSION['login']=$email; $_SESSION['id']=$num['id']; $_SESSION['name']=$uname; echo "<script type='text/javascript'> document.location = 'welcome.php'; </script>"; } else{ echo "<script>alert('Invalid Details');</script>"; } } ?> |
After successful login, you will redirect to welcome.php page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<?php session_start(); include_once("config.php"); if($_SESSION['login']) { ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <title>PHPGURUKUL | PHP Email Verification Script </title> <meta name="generator" content="Bootply" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link href="css/bootstrap.min.css" rel="stylesheet"> <!--[if lt IE 9]> <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link href="css/styles.css" rel="stylesheet"> </head> <body> <nav class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="navbar-header"> <a class="navbar-brand" rel="home" href="http://www.phpgurukul.com/">Home</a> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <div class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <li><a href="http://www.phpgurukul.com/all-demos/">All Demos</a></li> <li><a href="http://www.phpgurukul.com/category/php-projects/">Free Projects</a></li> <li><a href="http://www.phpgurukul.com/contact-us/">Contact</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Tutorials <b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="http://www.phpgurukul.com/category/php/">PHP </a></li> <li><a href="http://www.phpgurukul.com/category/php-oops-concepts/">PHP OOPs</a></li> <li class="divider"></li> <li><a href="http://www.phpgurukul.com/category/php-data-object/">PDO</a></li> <li class="divider"></li> <li><a href="http://www.phpgurukul.com/category/inteview-ques-ans/">Interview QA</a></li> </ul> </li> </ul> </div> </nav> <div class="container-fluid"> <div class="col-sm-6"> <div class="row"> <div class="col-xs-12"> <h3>PHP Email Verification Script </h3> <hr > <form name="insert" action="" method="post"> <table width="100%" border="0"> <tr><th>Welcome : <td ><font color="#FF0000"><?php echo $_SESSION['name']; ?></font> || <a href="logout.php">Logout</a></td> </tr> <tr> <th height="62" scope="row"> </th> <td width="71%"></td> </tr> </table> </form> </div> </div> <hr> </div><!--/center--> </div><!--/container-fluid--> <!-- script references --> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script src="js/bootstrap.min.js"></script> </body> </html> <?php } else { header('location:logout.php'); } ?> |
Code for Logout(logout.php)
1 2 3 4 5 6 7 8 9 10 |
<?php session_start(); $_SESSION['login']==""; session_unset(); $_SESSION['action1']="You have logged out successfully..!"; ?> <script language="javascript"> document.location="login.php"; </script> |
View Demo————————————————————-