About laravel5.4 generating verification code implementation codes

Source: Internet
Author: User
This article is mainly for everyone to bring an example of laravel5.4 generation verification code to explain. Small series feel very good, now share to everyone, also for everyone to make a reference. Follow the small series together to see it, hope to help everyone.

Summary: This article describes the steps to implement a verification code using Gregwar/captcha, as well as the problems and workarounds you might encounter.

Operation Steps:

1, locate the Composer.json file under the laravel5.4 project root directory,

Add to

"Gregwar/captcha": "1.*" to composer.json this file, as shown in.

2. Then open the command line, locate the project's root directory, run the composer update,

You can see that the extension library has been downloaded.

3. Next, you can use the verification code, first test the verification code can be displayed correctly,

Define the route first:

And then create a new codecontroller.php in the control layer,

<?php namespace App\http\controllers;use app\http\requests;use app\http\controllers\controller;use Illuminate\ http\request;//referencing the corresponding namespace use Gregwar\captcha\captchabuilder;use Session;class Codecontroller extends Controller{ Public Function Captcha ($temp) {  $builder = new Captchabuilder ();  $builder->build (150,32);  $phrase = $builder->getphrase ();  The contents are deposited  in Session session::flash (' Milkcaptcha ', $phrase);//Storage Verification Code  Ob_clean ();  Return Response ($builder->output ())->header (' Content-type ', ' image/jpeg '); }}

Then you can see the verification code by accessing the previously defined route in the browser.

In addition, it can be written in Composer.json,

The composer update is executed at the project root, and then the composer Dump-autoload command is executed.

The same effect can be achieved.

Finally, to say that I have encountered problems, many online generated laravel verification code pictures are written in this way,

Public Function Code ($TMP) {//Generate the Builder object for the CAPTCHA picture, configure the corresponding property $builder = new captchabuilder;//to set the picture width and font $builder->build ( $width =, $height = all, $font = null);//Get the contents of the verification code $phrase = $builder->getphrase ();//Deposit the Contents Sessionsession::flash (' Milkcaptcha ', $phrase);//Generate Picture Header ("Cache-control:no-cache, must-revalidate"); header (' Content-type:image/jpeg ') ; $builder->output ();}

I followed the test, the results of verification code image display garbled, do not display pictures, such as:

I changed it later.

Public Function Captcha ($temp) {  $builder = new Captchabuilder ();  $builder->build (150,32);  $phrase = $builder->getphrase ();  The contents are deposited  in Session session::flash (' Milkcaptcha ', $phrase);//Storage Verification Code  Ob_clean ();  Return Response ($builder->output ())->header (' Content-type ', ' image/jpeg '); }

It can be displayed normally.

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.