Examples of laravel5.4 verification code generation and laravel5.4 examples

Source: Internet
Author: User

Examples of laravel5.4 verification code generation and laravel5.4 examples

Summary: This article describes how to use gregwar/captcha to implement the verification code, as well as possible problems and solutions.

Procedure:

1. Find the file composer. json in the root directory of the laravel5.4 project,

Add

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

2. Open the command line, find the project root directory, and run composer update,

You can see that the extension library has been downloaded,

3. Next, you can use the verification code normally. First, test whether the verification code can be displayed normally,

First define the route:

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; // reference the corresponding namespace use Gregwar \ Captcha \ CaptchaBuilder; use Session; class CodeController extends Controller {public function captcha ($ temp) {$ builder = new CaptchaBuilder (); $ builder-> build (); $ phrase = $ builder-> getPhrase (); // Save the content to the session Session: flash ('milkcaptcha ', $ phrase ); // store the verification code ob_clean (); return response ($ builder-> output ()-> header ('content-type', 'image/jpeg ');}}

Then access the previously defined route in the browser and you will be able to see the verification code.

In addition, it can be written in composer. json as follows,

Or execute composer update in the project root directory, and then execute the composer dump-autoload command.

The same effect can be achieved.

Finally, let's talk about the problems I encountered. Many images that generate laravel verification codes on the Internet are written in this way,

Public function code ($ 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 sessionSession: flash ('milkcaptcha ', $ phrase ); // generate the image header ("Cache-Control: no-cache, must-revalidate"); header ('content-Type: image/jpeg '); $ builder-> output ();}

I tried it, and the result verification code is garbled and the picture is not displayed, for example:

Later, I changed it.

Public function captcha ($ temp) {$ builder = new CaptchaBuilder (); $ builder-> build (150,32); $ phrase = $ builder-> getPhrase (); // Save the content to the session Session: flash ('milkcaptcha ', $ phrase); // store the verification code ob_clean (); return response ($ builder-> output ()) -> header ('content-type', 'image/jpeg ');}

It will be displayed normally.

The above example of laravel5.4 verification code generation is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's home.

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.