Download
When users sign up to join your website, you may want to verify their email address by sending confirmation link to their email address. You'll learn how to do this in this tutorial.In this tutorial, We will create main 8 PHP files and 2 DB tables
- index.php
- home.php
- register.php
- signup_ac.php
- sendmail.php
- confirmation.php
- logout.php
- db_conection.php
We will create 2 DB tabes
- temp_users
- users
What to do
When users sign up. Random a set of confirmation code.
- Keep their informations and confirmation code in table "temp_users" table. This is temporary table, we have to move this informations to table "users" after email address has been verified.
- After sucessfully inserted data into table "temp_users", send confirmation link to email that users used to sign up, if email is invalid they will not receive our email.
- They have to click on confirmation link to activate their account. (move data from table "temp_users" to table "users" and delete data from table "temp_users" in this step)
NOTE: In this script we are using PHPMailer to send mail even from the localhost. because in local server you can not send mail with PHP Mail Function. We also set up Login and Logout functionality with Session Management.
STEP1: Create table "temp_users" and table "users"
CREATE TABLE IF NOT EXISTS `temp_users` ( `id` int(11) NOT NULL, `confirm_code` varchar(100) NOT NULL, `user_name` varchar(100) NOT NULL, `user_pass` varchar(50) NOT NULL, `user_email` varchar(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL, `user_name` varchar(100) NOT NULL, `user_pass` varchar(50) NOT NULL, `user_email` varchar(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
STEP2: register.php - Create Registration form
<?php session_start(); if (isset($_SESSION['user']) != "") { header("Location: home.php"); } include_once ("header.php"); ?> <body> <div class="container"> <div class="row"> <div class="login-panel panel panel-success"> <div class="panel-heading"> <h3 class="panel-title text-center">Registration</h3> </div> <div class="panel-body"> <form enctype="multipart/form-data" id="register_user" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"> <fieldset> <div class="form-group"> <label for="registerusername11">User Name <span class="red">*</span></label> <input type="text" placeholder="Username" name="name" value="" class="form-control" required autofocus> </div> <div class="form-group"> <label for="registeremail11">E-mail <span class="red">*</span></label> <input type="email" placeholder="E-mail" name="email" value="" class="form-control" autofocus required> </div> <div class="form-group"> <label for="registerpass11">Password <span class="red">*</span></label> <input class="form-control" placeholder="Password" name="pass" type="password" id="password" value="" required> </div> <div class="form-group"> <label for="retypepass11">Re-type Password <span class="red">*</span></label> <input class="form-control" placeholder="Re-type Password" name="retypepass" type="password" id="retypepassword" value="" required> </div> <button id="register" name="register" class="btn btn-lg btn-success btn-block" type="submit"><i class="fa fa-check" aria-hidden="true"></i> Register</button> </fieldset> </form> <center><b>Already registered ?</b> <br><a href="index.php">Login here</a></center><!--for centered text--> </div> </div> </div> </div> <script> jQuery(document).ready(function() { jQuery("#register_user").submit(function(e) { e.preventDefault(); var password = jQuery('#password').val(); var cpassword = jQuery('#retypepassword').val(); if (password == cpassword) { var formData = jQuery(this).serialize(); $.ajax({ type : "POST", url : "signup_ac.php", data : formData, beforeSend: function() { $.jGrowl("Please Wait", { header : 'Loading..' }); }, success : function(html) { var finalresult = html.trim(); if (finalresult == 'true') { $.jGrowl("Welcome to User Registration Management System", { header : 'Registration Successful' }); var delay = 2000; setTimeout(function() { window.location = 'home.php' }, delay); } else if (finalresult == 'false') { $.jGrowl("User already exists in our database. Please Sure to Check Your Email ID with which You Belong. ", { header : 'Registration Failed' }); } } }); } else { $.jGrowl("Your confirm password does not match with current password", { header : 'Register Failed' }); } }); }); </script> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="lib/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-jgrowl/1.4.5/jquery.jgrowl.min.js"></script> </body> </html>
STEP3: signup_ac.php - Insert data into database
In this step:- Create Random confirmation code.
- Insert data and confirmation code into database.
- Send email to user with confirmation link.
<?php session_start(); include_once("database/db_conection.php"); //here getting result from the post array after submitting the form. $user_name = mysqli_real_escape_string($db_conn, $_POST['name']); $user_pass = md5(mysqli_real_escape_string($db_conn, $_POST['pass'])); $user_email = mysqli_real_escape_string($db_conn, $_POST['email']); //here query check weather if user already registered so can't register again. $check_email_query = "select * from temp_users WHERE user_email='$user_email'"; $result = mysqli_query($db_conn, $check_email_query); $row = mysqli_fetch_array($result); $count = mysqli_num_rows($result); $confirm_code = md5(unixtojd(rand())); if ($count <= 0){ mysqli_query($db_conn,"insert into temp_users (confirm_code,user_name,user_pass,user_email) VALUE ('$confirm_code','$user_name','$user_pass','$user_email')")or die(mysqli_error($db_conn)); $_SESSION['user']=$user_name; $_SESSION['user_email']=$user_email; include_once("sendmail.php"); }else{ echo 'false'; } ?>
STEP4: confirmation.php
When user open his email he'll see this message and link to file "confirmation.php" including passkey in url.
In this step:
In this step:
- Check passkey
- If found passkey in database, move all data in that row from table "temp_users" to table "users"
- Delete passkey from table "temp_users"
<?php include_once("database/db_conection.php"); $result2 = $result1 = null; // Passkey that got from link $passkey=$_GET['passkey']; $tbl_name1="temp_users"; // Retrieve data from table where row that match this passkey $sql1="SELECT * FROM $tbl_name1 WHERE confirm_code ='$passkey'"; $result1=mysqli_query($db_conn,$sql1); // If successfully queried if($result1){ // Count how many row has this passkey $count=mysqli_num_rows($result1); // if found this passkey in our database, retrieve data from table "temp_users" if($count==1){ $rows=mysqli_fetch_array($result1); $name=$rows['user_name']; $email=$rows['user_email']; $password=$rows['user_pass']; $tbl_name2="users"; // Insert data that retrieves from "temp_users" into table "users" $sql2="INSERT INTO $tbl_name2(user_name, user_email, user_pass)VALUES('$name', '$email', '$password')"; $result2=mysqli_query($db_conn,$sql2); } // if not found passkey, display message "Wrong Confirmation code" else { echo "Wrong Confirmation code"; } // if successfully moved data from table"temp_users" to table "users" displays message "Your account has been activated" and don't forget to delete confirmation code from table "temp_members_db" if($result2){ echo "Your account has been activated"; // Delete information of this user from table "temp_members_db" that has this passkey $sql3="DELETE FROM $tbl_name1 WHERE confirm_code = '$passkey'"; $result3=mysqli_query($db_conn,$sql3); } } ?>
https://github.com/vijayrami/user_registration_with_Verifying_email_address
0 Comments