Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of PHP Mailbox Verification

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you PHP mailbox verification example analysis, I believe most people do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Let's start with a registration form:

First Name: Last Name: Last name: Password: Confirm Password: Register

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 '0', `verify_token` VARCHAR(255) , `created_at` TIMESTAMP, `updated_at` TIMESTAMP,);

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

// 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 pageif($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 databaseif(!$ 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 messagereturn 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. ');

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

The whole process is very simple-when a new user is created, an email containing a verification link is sent to the user's email address during the registration process. Users cannot log in and use the website application until they click on the email verification link and confirm their email address.

There are a few things to note about validation links. Validated links need to contain a randomly generated token, which should be long enough and valid only for a certain period of time, in order to prevent network attacks. Mailbox authentication also needs to include the user's unique identification, so as to avoid the potential dangers of attacking multiple users.

Now let's see how to generate a validation 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;

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

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');});

Content of mailbox verification:

Please confirm your e-mail address by clicking the following link:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report