Table of Contents
PHP email verification example tutorial, php email example
Home Backend Development PHP Tutorial PHP email verification example tutorial, php email example_PHP tutorial

PHP email verification example tutorial, php email example_PHP tutorial

Jul 12, 2016 am 08:51 AM
php Mail verify

PHP email verification example tutorial, php email example

One of the most common security verifications in user registration 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’s start with a registration 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

26

27

28

29

30

<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

Next is the table structure of the database:

1

2

3

4

5

6

7

8

9

10

11

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 '0',

 `verify_token` VARCHAR(255) ,

 `created_at` TIMESTAMP,

 `updated_at` TIMESTAMP,

);

Copy after login

Once the form is submitted, we need to validate the user's input and create a new user:

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

// Validation rules

$rules = array(

  'fname' => 'required|max:255',

  'lname' => 'required|max:255',

 'email' => 'required',

 'password' => 'required|min:6|max:20',

 'cpassword' => 'same:password'

);

 

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

 

// If input not valid, go back to registration page

if($validator->fails()) {

 return Redirect::to('registration')->with('error', $validator->messages()->first())->withInput();

}

 

$user = new User();

$user->fname = Input::get('fname');

$user->lname = Input::get('lname');

$user->password = Input::get('password');

 

// 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('registration')->with('error', 'Unable to write to database at this time. Please try again later.')->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('registration')->with('success', '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.');

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:

1

2

3

4

// 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:

1

2

3

4

Mail::send('emails.email-confirmation', array('code' => $code, 'id' => $user->id), function($message)

{

$message->from('my@domain.com', 'Mydomain.com')->to($user->email, $user->fname . ' ' . $user->lname)->subject('Mydomain.com: E-mail confirmation');

});

Copy after login

Contents of email verification:

1

2

3

4

5

6

7

8

9

10

11

12

13

<!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&#63;code=<&#63;php echo $code; &#63;>&user=<&#63;php echo $id; &#63;>"></a>

 </p>

 </body>

</html>

Copy after login

Now let’s verify if it works:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

$user = User::where('id', '=', Input::get('user'))

  ->where('is_active', '=', 0)

  ->where('verify_token', '=', Input::get('code'))

  ->where('created_at', '>=', 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('verify')->with('error', 'Unable to connect to database at this time. Please try again later.');

 }

 

 // Show the success message

 return Redirect::to('verify')->with('success', 'You account is now active. Thank you.');

}

 

// Code not valid, show error message

return Redirect::to('verify')->with('error', 'Verification code not valid.');

Copy after login

Conclusion:
The code shown above is just a tutorial example and has not been adequately tested. 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.

This article is an original translation by PHPChina. The original text is reprinted at http://www.phpchina.com/portal.php?mod=view&aid=39888. The editor believes that this article is of great learning value and would like to share it with everyone. I hope it will be useful to everyone. Everyone’s learning helps.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133031.htmlTechArticlePHP email verification example tutorial, php email example One of the most common security verifications in user registration is email verification. According to the general industry practice, email verification is to avoid potential security...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Top 10 Digital Virtual Currency Apps Rankings: Top 10 Digital Currency Exchanges in Currency Circle Trading Top 10 Digital Virtual Currency Apps Rankings: Top 10 Digital Currency Exchanges in Currency Circle Trading Apr 22, 2025 pm 03:00 PM

The top ten digital virtual currency apps are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.

How to register an account on Ouyi Exchange Ouyi Exchange Registration Tutorial How to register an account on Ouyi Exchange Ouyi Exchange Registration Tutorial Apr 24, 2025 pm 02:06 PM

The steps to register an Ouyi account are as follows: 1. Prepare a valid email or mobile phone number and stabilize the network. 2. Visit Ouyi’s official website. 3. Enter the registration page. 4. Select email or mobile phone number to register and fill in the information. 5. Obtain and fill in the verification code. 6. Agree to the user agreement. 7. Complete registration and log in, carry out KYC and set up security measures.

Top 10 digital currency exchanges Top 10 digital currency app exchanges Top 10 digital currency exchanges Top 10 digital currency app exchanges Apr 22, 2025 pm 03:15 PM

The top ten digital currency exchanges are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.

Binance download link Binance download path Binance download link Binance download path Apr 24, 2025 pm 02:12 PM

To safely download the Binance APP, you need to go through the official channels: 1. Visit the Binance official website, 2. Find and click the APP download portal, 3. Choose to scan the QR code, app store, or directly download the APK file to download to ensure that the link and developer information are authentic, and enable two-factor verification to protect the security of the account.

Download the official website of Ouyi Exchange app for Apple mobile phone Download the official website of Ouyi Exchange app for Apple mobile phone Apr 28, 2025 pm 06:57 PM

The Ouyi Exchange app supports downloading of Apple mobile phones, visit the official website, click the "Apple Mobile" option, obtain and install it in the App Store, register or log in to conduct cryptocurrency trading.

Top 10 digital virtual currency trading app rankings Top 10 digital currency exchange rankings in 2025 Top 10 digital virtual currency trading app rankings Top 10 digital currency exchange rankings in 2025 Apr 22, 2025 pm 02:45 PM

The top ten digital currency exchanges are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.

How to register an account on Sesame Open Exchange? Tutorial on Registration of Sesame Open Exchange How to register an account on Sesame Open Exchange? Tutorial on Registration of Sesame Open Exchange Apr 24, 2025 pm 02:00 PM

Registering a Sesame Door Account requires 7 steps: 1. Prepare a valid email or mobile phone number and a stable network; 2. Visit the official website; 3. Enter the registration page; 4. Select and fill in the registration method; 5. Obtain and fill in the verification code; 6. Agree to the user agreement; 7. Complete registration and log in, it is recommended to carry out KYC and set security measures.

How to register an account on Binance Exchange Binance Exchange Registration Tutorial How to register an account on Binance Exchange Binance Exchange Registration Tutorial Apr 24, 2025 pm 02:03 PM

The steps to register a Binance account include: 1. Prepare a valid email or mobile phone number and a stable network; 2. Visit Binance official website; 3. Enter the registration page; 4. Select the registration method; 5. Fill in the registration information; 6. Agree to the user agreement; 7. Complete verification; 8. Obtain and fill in the verification code; 9. Complete registration.

See all articles