Laravel5.2 use Captcha to generate a verification code for Logon (session pitfall), laravel5.2captcha

Source: Internet
Author: User

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:


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.


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!

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