Send A Verification Email When A New User Registered

Send A verification email when a new user registered  image

When a new user  registered for the first time the account must verified. It can be done in different ways. 

// REGISTER USER
if (isset($_POST['register_btn'])) {
// receive all input values from the form
$firstname = esc($_POST['firstname']);
$lastname = esc($_POST['lastname']);
$genders=esc($_POST["gender"]);
$username = esc($_POST['username']);
$email = esc($_POST['email']);
$password_1 = esc($_POST['password_1']);
$password_2 = esc($_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($firstname)) {  array_push($errors, "Uhmm...You missed your First Name"); }
if (empty($lastname)) {  array_push($errors, "Uhmm...You missed your Last Name"); }
if (empty($username)) {  array_push($errors, "Uhmm...We gonna need your username"); }
if (empty($email)) { array_push($errors, "Oops.. Email is missing");}                                    
if (empty($password_1)) { array_push($errors, "uh-oh you forgot the password"); }
if ($password_1 != $password_2) { array_push($errors, "The two passwords do not match");}
// Ensure that no user is registered twice. 
// the email and usernames should be unique
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($conn, $user_check_query);
$user = mysqli_fetch_assoc($result);    
		if ($user) { // if user exists
			if ($user['username'] === $username) {
			  array_push($errors, "Username already exists");
			}
			if ($user['email'] === $email) {
			  array_push($errors, "Email already exists");
			}
		}
		// register user if there are no errors in the form
		if (count($errors) == 0) {
			$password = md5($password_1);//encrypt the password before saving in the database
			$vkey = md5(time().$username); 
			$cenvertedTime =date('Y-m-d', strtotime(' + 5 days'));
			$query = "INSERT INTO users (firstname, lastname, gender, username, email, password, vkey, token_expire, created_at, updated_at) 
					  VALUES('$firstname', '$lastname', '$genders', '$username', '$email', '$password', '$vkey', '$cenvertedTime', now(), now())";
			$resultset = mysqli_query($conn, $query);
			
			$inserted_id = mysqli_insert_id($conn);
			$user_check = "SELECT token_expire FROM users WHERE id='$inserted_id' LIMIT 1";
            $result = mysqli_query($conn, $user_check);
		    $date = mysqli_fetch_assoc($result);
            $created_at = $date['token_expire'];
			$created_at = date("F j, Y ", strtotime($created_at));
			
        if($resultset){
        $auto = date (Y);
		$to=$email;
		$sent_date = date("F j, Y ");
		$subject='Please confirm your email address';
		$message="

let's get thingis together, finally send then email

	$message="include your custom body design/ I included message body in previous post";
		$headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From: Do-Not-Reply noreply@g3techdesign.com'. "\r\n";
         
		mail($to, $subject, $message, $headers);
        
		$_SESSION['message'] = " Thank you for signing up in to our system. We have sent a verification email to the address you provide.
". $to . " is the email address you provide to us to activate your account.";
		
     	// redirect to public area
		header('location: '. BASE_URL . 'thankyou.php?email=' . $email);				
		exit(0);			
			}
			 else {
				$_SESSION['message'] = "Something want wrong.";
				// redirect to public area
				header('location: '. BASE_URL . 'signup.php');				
				exit(0);
			}
    }
}

Remove empty space from the form

// escape value from form
	function esc(String $value)
	{	
		// bring the global db connect object into function
		global $conn;

		$val = trim($value); // remove empty space sorrounding string
		$val = mysqli_real_escape_string($conn, $value);

		return $val;
	}

Then when a user click on the link they have recieved the account will activate  then they can able to login.

$errors = array(); 
if(isset($_GET['vkey'])){
	$vkey = $_GET['vkey'];
	$user_check = "SELECT verified,vkey, token_expire FROM users WHERE verified=0 AND vkey = '$vkey' LIMIT 1";
        $result = mysqli_query($conn, $user_check);
        $get_token = mysqli_fetch_assoc($result);
	  if($result->num_rows == 1){
	      $currentDateTime = date('Y-m-d H:i:s');
	    $token_date =$get_token['token_expire'];
	  if($currentDateTime < $token_date){
	  $sql = "UPDATE users SET verified=1 WHERE vkey='$vkey' LIMIT 1";
          $results = mysqli_query($conn, $sql);
	  $_SESSION['message'] = " Your Acount has been verified. You may now login in to your Account.";
      header('location: ' . BASE_URL . 'login.php');
      exit(0);
		}else{
		array_push($errors, " Verification link has been expired. You may attempt to create your account again. The account you created was deleted automatically");
		}
	  }else{
		array_push($errors, " This Account is already verified or invalid. You may try to login now.");
		}
}

that's.

Leave a non public comment how to improve it.



Characters Remaining

We are sorry for your bad experiance. Leave a non public comment how to improve it.



Characters Remaining

Related Posts (12)

Getting User Device Location and Updating Database image

Getting User Device Location and Updating Database

Author image
By JOHN Mon Feb 19, 2024 at 12:31 PM (3 months ago)
Updated On Sat Mar 09, 2024 at 07:59 AM (2 months ago)

Read More »
How to create a custom archive page template on your website layout image

How to create a custom archive page template on your website layout

Author image
By JOHN Thu Nov 17, 2022 at 08:00 AM (2 years ago)
Updated On Thu Jan 05, 2023 at 06:24 PM (one year ago)

Read More »
Test the video layout design(Upload and Store video to MySQL Database with PHP) image

Test the video layout design(Upload and Store video to MySQL Database with PHP)

Author image
By JOHN Sat Nov 05, 2022 at 05:15 PM (2 years ago)
Updated On Wed Nov 09, 2022 at 06:39 PM (2 years ago)

Read More »
How to set the expiry period for a reset password link. image

How to set the expiry period for a reset password link.

Author image
By JOHN Mon Oct 10, 2022 at 05:31 AM (2 years ago)
Updated On Tue Nov 08, 2022 at 07:53 PM (2 years ago)

Read More »
How to Send Verification link when a new user register image

How to Send Verification link when a new user register

Author image
By JOHN Fri Oct 15, 2021 at 07:49 AM (3 years ago)
Updated On Fri Nov 04, 2022 at 05:00 PM (2 years ago)

This tutorial teaches you to build an email verification script from scratch

Read More »
How to limit the number of login attempts  image

How to limit the number of login attempts

Author image
By JOHN Sat Oct 31, 2020 at 10:19 PM (4 years ago)
Updated On Wed Nov 09, 2022 at 04:38 PM (2 years ago)

It is one of a security mechanism to restrict an authorized user from access the systems and locked the account if it's necessary. In this tutorial we will create a simple login system to demonstrate the implementation using PHP and MySQL. Let's digest it...

Read More »
How to limit the number of login attempt using PHP part 2(PHP functionality) image

How to limit the number of login attempt using PHP part 2(PHP functionality)

Author image
By JOHN Tue Sep 22, 2020 at 06:47 PM (4 years ago)
Updated On Wed Nov 09, 2022 at 05:06 PM (2 years ago)

To protect spam or junk email registration the system have to verify user email by sending a verification link to a  proven email address[] 

Read More »
Download file using PHP image

Download file using PHP

Author image
By MEWDED Wed Sep 02, 2020 at 10:04 AM (4 years ago)
Updated On Sat Nov 12, 2022 at 09:10 AM (2 years ago)

Short explanation 

Read More »
Inactive user enforce to login (SESSION expired) or Limmit the resource image

Inactive user enforce to login (SESSION expired) or Limmit the resource

Author image
By MEWDED Tue Sep 01, 2020 at 04:24 AM (4 years ago)
Updated On Sat Nov 12, 2022 at 09:35 AM (2 years ago)

Quick description

Read More »
How to count page viewers based on the IP Address of the device image

How to count page viewers based on the IP Address of the device

Author image
By MEWDED Thu Aug 27, 2020 at 04:37 PM (4 years ago)
Updated On Sat Nov 12, 2022 at 06:57 PM (2 years ago)

Hi, today we will walk you through how to implement to count the page viewers base on IP address like Youtube or Facebook 

Read More »
Pagination in PHP image

Pagination in PHP

Author image
By MEWDED Fri Aug 14, 2020 at 02:58 PM (4 years ago)
Updated On Fri Nov 04, 2022 at 03:08 PM (2 years ago)

Hi there ????, 

TODAY we are walking ????‍?? together to show you how to implement pagination in PHP

Read More »
Author Title image

Author Title

Author image
By JOHN Mon Jul 13, 2020 at 04:19 AM (4 years ago)
Updated On Fri Nov 04, 2022 at 03:08 PM (2 years ago)

Author description

Read More »

Share this on

Search


Archives

No archives data found yet in 2025.

No archives data found in 2016.

Find Us on Facebook

Subscribe for new updates




Back to Top