Laravel5.2 use Captcha to generate a verification code for Logon (session pitfall), laravel5.2captcha
Recently, a friend asked me to help with laravel's verification code login, so I did a little research. (I forgot how to use laravel)
First, install laravel and I will not go into details next. My version is 5.2.45 (note: the middleware of laravel5.2.6 or later can be automatically loaded). This is quite important.
After the installation is complete, you need to use composer to load your Captcha. The specific method is in your composer. add "gregwar/captcha": "1. * "this line of code. Then, run the command cmd in the root directory of your project to run the composer update code line. In this way, you can install the library or middleware. Then you can write your code freely.
Php: (do not repeat the specific routing in the next step. Only write the key code)
Public function captcha ($ tmp) {// Builder object for generating the verification code image, and configure the corresponding attribute $ builder = new CaptchaBuilder; // you can set the image width and height and font $ builder-> build ($ width = 100, $ height = 40, $ font = null ); // obtain the content of the Verification Code $ phrase = $ builder-> getPhrase (); // Save the content to the session Session: flash ('milkcaptcha ', $ phrase ); // generate the image header ("Cache-Control: no-cache, must-revalidate"); header ('content-Type: image/jpeg '); $ builder-> output ();}
Call the blade template:
If you think this is all done, you are actually too yang too simple. During your verification, you will find that everything is wrong.
Is it sour. In laravel5.2, all sessions cannot be cross-controller or method. If they are cross-controller, the session will be re-generated. By default, the session must be passed through the middleware. Don't worry. The next method is to solve the problem. Currently, I know there are two solutions. One is to build a middleware and store all sessions in it, but it is a little troublesome. Next I will introduce a simple method, in your laravel \ app \ Http \ Kernel. add the following code to $ middleware in the php file:
\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,
Then you can safely use the session. This is the next verification code method,
Public function login_data () {$ userInput = \ Request: get ('captcha '); if (Session: get ('milkcaptcha') = $ userInput) {// The user entered the verification code correctly. Verify your own password username echo 1;} else {// The user entered the verification code incorrectly echo 2 ;}}
In the end, I had to comment out laravel's official documentation, which is not described in advance. I guess it is a worry that developers are too comfortable to develop.
Summary
The above section describes Laravel5.2's use of Captcha to generate a verification code for Logon (session). I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!