laravel5.2 use CAPTCHA to generate verification code (session giant pit)

Source: Internet
Author: User
Tags sessions

recently, a friend asked me to help get the Laravel verification code landing, so a little research. (I almost forgot how to use the Laravel)

first of all, install laravel do not have to repeat, my version is 5.2.45 (note: laravel5.2.6 above the version of middleware can automatically load), which 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. Then, in your project root directory, use CMD to run composer update this line of code. This way, even if the library is installed or middleware. Then you can be presumptuous to write your code.

php: (The specific route of what the next do not do too much to repeat, write only key code)

The Public Function captcha ($tmp)
{
    //generates the builder object for the Captcha picture, configures the corresponding property
    $builder = new Captchabuilder;
    You can set the picture wide and font
    $builder->build ($width =, $height =, $font = null);
    Gets the contents of the authentication code
    $phrase = $builder->getphrase ();

    The content is deposited
    in session session::flash (' Milkcaptcha ', $phrase);
    Generate
    a picture header ("Cache-control:no-cache, must-revalidate");
    Header (' Content-type:image/jpeg ');
    $builder->output ();
}

call to Blade Template:
  If you think it's done, you're really too yang too. You'll find out what's wrong when you verify it. is
not very sour. Because in laravel5.2, all the sessions are not across the controller, methods, if the cross, the session will be reborn
, the default sessions 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 sessions are stored in it, but a little trouble, next I introduce a simple
method, in your laravel\app\http\kernel.php Add the following code to the $middleware in the 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 rest assured that the use of the session. This is the way to verify the code in the next

Public Function Login_data ()
{
    $userInput = \request::get (' captcha ');

    if (Session::get (' milkcaptcha ') = = $userInput) {//
        user input Verification code correct, verify your password username      echo 1;
    } else {
        // User input authentication code error
        echo 2
    }
}

Finally have to spit out the official documents of Laravel, such as this kind of pit is not in advance, it is estimated that developers are worried about the development of it.
End ...
I wish you all a smooth work, happy every day, everyone has their own small objects, long and very kind of ...

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.