Detailed explanation of PHP email verification examples

墨辰丷
Release: 2023-03-29 11:12:02
Original
1789 people have browsed it

This article mainly introduces PHP email verification examples in detail, and leads you step by step through examples to understand the process of PHP email verification. Interested friends can refer to

The most important steps in user registration One of the common security verifications is email verification. According to common industry practices, email verification is a very important practice to avoid potential security risks. Let us now discuss these best practices and see how to create an email verification in PHP.

Let us start with a registration form:

<form method="post" action="http://mydomain.com/registration/">
 <fieldset class="form-group">
 <label for="fname">First Name:</label>
 <input type="text" name="fname" class="form-control" required />
  </fieldset>

  <fieldset class="form-group">
 <label for="lname">Last Name:</label>
 <input type="text" name="lname" class="form-control" required />
  </fieldset>

  <fieldset class="form-group">
 <label for="email">Last name:</label>
 <input type="email" name="email" class="form-control" required />
  </fieldset>

  <fieldset class="form-group">
 <label for="password">Password:</label>
 <input type="password" name="password" class="form-control" required />
  </fieldset>

  <fieldset class="form-group">
 <label for="cpassword">Confirm Password:</label>
 <input type="password" name="cpassword" class="form-control" required />
  </fieldset>

  <fieldset>
    <button type="submit" class="btn">Register</button>
  </fieldset>
</form>
Copy after login

The next is the table structure of the database:

CREATE TABLE IF NOT EXISTS `user` (
 `id` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
 `fname` VARCHAR(255) ,
 `lname` VARCHAR(255) ,
 `email` VARCHAR(50) ,
 `password` VARCHAR(50) ,
 `is_active` INT(1) DEFAULT &#39;0&#39;,
 `verify_token` VARCHAR(255) ,
 `created_at` TIMESTAMP,
 `updated_at` TIMESTAMP,
);
Copy after login

Once this form is submitted, we need to verify the user's Enter and create a new user:

// Validation rules
$rules = array(
  &#39;fname&#39; => &#39;required|max:255&#39;,
  &#39;lname&#39; => &#39;required|max:255&#39;,
 &#39;email&#39; => &#39;required&#39;,
 &#39;password&#39; => &#39;required|min:6|max:20&#39;,
 &#39;cpassword&#39; => &#39;same:password&#39;
);

$validator = Validator::make(Input::all(), $rules);

// If input not valid, go back to registration page
if($validator->fails()) {
 return Redirect::to(&#39;registration&#39;)->with(&#39;error&#39;, $validator->messages()->first())->withInput();
}

$user = new User();
$user->fname = Input::get(&#39;fname&#39;);
$user->lname = Input::get(&#39;lname&#39;);
$user->password = Input::get(&#39;password&#39;);

// You will generate the verification code here and save it to the database

// Save user to the database
if(!$user->save()) {
 // If unable to write to database for any reason, show the error
 return Redirect::to(&#39;registration&#39;)->with(&#39;error&#39;, &#39;Unable to write to database at this time. Please try again later.&#39;)->withInput();
}

// User is created and saved to database
// Verification e-mail will be sent here

// Go back to registration page and show the success message
return Redirect::to(&#39;registration&#39;)->with(&#39;success&#39;, &#39;You have successfully created an account. The verification link has been sent to e-mail address you have provided. Please click on that link to activate your account.&#39;);
Copy after login

After registration, the user's account remains invalid until the user's email is verified. This feature confirms that the user is the owner of the entered email address and helps prevent spam and unauthorized email use and information disclosure.

The whole process is very simple - when a new user is created, an email containing a verification link will be sent to the email address filled in by the user during the registration process. Before the user clicks the email verification link and confirms the email address, the user cannot log in and use the website application.

There are several things to note about verified links. The verified link needs to contain a randomly generated token that is long enough and only valid for a certain period of time. This is done to prevent network attacks. At the same time, the email verification also needs to include the user's unique identifier, so as to avoid potential dangers of attacking multiple users.

Now let's see how to generate a verification link in practice:

// We will generate a random 32 alphanumeric string
// It is almost impossible to brute-force this key space
$code = str_random(32);
$user->confirmation_code = $code;
Copy after login

Once this verification is created, store it in the database and send it to the user:

Mail::send(&#39;emails.email-confirmation&#39;, array(&#39;code&#39; => $code, &#39;id&#39; => $user->id), function($message)
{
$message->from(&#39;my@domain.com&#39;, &#39;Mydomain.com&#39;)->to($user->email, $user->fname . &#39; &#39; . $user->lname)->subject(&#39;Mydomain.com: E-mail confirmation&#39;);
});
Copy after login

Contents of email verification:

<!DOCTYPE html>
<html lang="en-US">
 <head>
 <meta charset="utf-8" />
 </head>

 <body>
 <p style="margin:0">
  Please confirm your e-mail address by clicking the following link:
  <a href="http://mydomain.com/verify?code=<?php echo $code; ?>&user=<?php echo $id; ?>"></a>
 </p>
 </body>
</html>
Copy after login

Now let’s verify whether it is possible:

$user = User::where(&#39;id&#39;, &#39;=&#39;, Input::get(&#39;user&#39;))
  ->where(&#39;is_active&#39;, &#39;=&#39;, 0)
  ->where(&#39;verify_token&#39;, &#39;=&#39;, Input::get(&#39;code&#39;))
  ->where(&#39;created_at&#39;, &#39;>=&#39;, time() - (86400 * 2))
  ->first();

if($user) {
 $user->verify_token = null;
 $user->is_active = 1;

 if(!$user->save()) {
 // If unable to write to database for any reason, show the error
 return Redirect::to(&#39;verify&#39;)->with(&#39;error&#39;, &#39;Unable to connect to database at this time. Please try again later.&#39;);
 }

 // Show the success message
 return Redirect::to(&#39;verify&#39;)->with(&#39;success&#39;, &#39;You account is now active. Thank you.&#39;);
}

// Code not valid, show error message
return Redirect::to(&#39;verify&#39;)->with(&#39;error&#39;, &#39;Verification code not valid.&#39;);
Copy after login

Conclusion:
The code shown above is just a tutorial example , and doesn't pass enough testing. Please test it before using it in your web application. The above code is done in the Laravel framework, but you can easily migrate it to other PHP frameworks. At the same time, the verification link is valid for 48 hours and expires after that. Introducing a work queue can handle expired verification links in a timely manner.

The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Identity card verification code calculation method based on PHP

The running time of four basic sorting algorithms implemented in PHP Comparison (must read)

A method of using locking to implement the code grabbing function under concurrency based on PHP

The above is the detailed content of Detailed explanation of PHP email verification examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!