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.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!