Laravel5.2 using CAPTCHA to generate a verification code for login

Source: Internet
Author: User
This article mainly describes the Laravel5.2 use CAPTCHA Generate verification code (session pit), the need for friends can refer to the next

Recently a friend asked me to help out. Laravel's verification code landing, so a little study of a bit. (I almost forgot how to use Laravel.)

First of all, the installation of Laravel will not have to repeat it, my version is 5.2.45 (note: laravel5.2.6 version of the middleware can be loaded automatically), this is very important.

After the installation is complete, you need to use composer to load your captcha by adding "Gregwar/captcha" to the Require array in your Composer.json: "1.*" line of code. Then, use cmd in your project root directory to run the composer update line of code. This way, even if the library is installed or middleware. After that, you can easily write your code.

PHP: (Specific route what the next do not do too much to repeat, only write the key code)

Public Function Captcha ($tmp) {  //Generate the Builder object for the CAPTCHA picture, configure the corresponding property  $builder = new Captchabuilder;  You can set the picture width height and font  $builder->build ($width =, $height = +, $font = null);  Get the contents of the captcha  $phrase = $builder->getphrase ();  Deposit the content  in Session session::flash (' Milkcaptcha ', $phrase);  Generate Picture  header ("Cache-control:no-cache, must-revalidate");  Header (' Content-type:image/jpeg ');  $builder->output ();}

Blade a template call:

If you think it's done, then you really are too yang too simple. You will find out what is wrong when you verify it.

is not very sour. Because in the laravel5.2, all the sessions are not across the controller, the method, if the cross, the session will be regenerated, the default session of the delivery needs to pass through the middleware. Don't worry, the next way is to solve the problem. At present I know there are two solutions, one is to build a middleware, and then all the session is stored in it, but a little trouble, then I introduce a simple method, in your laravel\app\http\kernel.php file of $ Add the following code in the middleware:

\illuminate\foundation\http\middleware\checkformaintenancemode::class,\illuminate\cookie\middleware\ encryptcookies::class,\illuminate\cookie\middleware\addqueuedcookiestoresponse::class,\illuminate\session\ Middleware\startsession::class,\illuminate\view\middleware\shareerrorsfromsession::class,

After that, you can use the session with confidence. This is the way to verify the code under,

Public Function Login_data () {  $userInput = \request::get (' captcha ');  if (Session::get (' milkcaptcha ') = = $userInput) {    //user Input Verification code is correct, verify your own password user name    echo 1;  } else {    //user input CAPTCHA Error    echo 2;}  }

Finally have to spit the Laravel official documents, such as the pit is not explained in advance, it is estimated that developers are too good to develop.

Summarize

The above is a small part of the Laravel5.2 to introduce the use of CAPTCHA generated verification code to achieve login (session giant pit), I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to everyone. Thank you very much for the support of PHP Chinese network!

Articles you may be interested in:

A detailed description of the bucket ordering of the PHP sorting algorithm series

PHP sorting algorithm series of merge sort detailed _php tips

A detailed description of the direct selection sequence of the PHP sorting algorithm series

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.