Laravel5.4 generates the verification code and laravel5.4 the verification code.
Summary: This blog introduces how to use gregwar/captcha to implement the verification code, as well as possible problems and solutions. Http://www.cnblogs.com/zbokett/p/7287235.html
Reprinted, please specify the source !!!
Address: http://www.cnblogs.com/zbokett/p/7287235.html
Procedure:
1. Find the file composer. json in the root directory of the laravel5.4 project,
Add "gregwar/captcha": "dev-master" and "Gregwar \ Captcha \": "vendor/Captcha/" to the composer. json file, as shown in,
Next, execute composer update in the project root directory, and then execute the composer dump-autoload command.
Method 2: http://www.cnblogs.com/zbokett/p/7287235.html
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 define the route:
Next, we will introduce two types of verification codes,
I. Output The Verification Code image directly on the webpage
Http://www.cnblogs.com/zbokett/p/7287235.html
Create a new codeController. php In the control layer, 1 <? Php 2 namespace App \ Http \ Controllers;
3 4 use App \ Http \ Requests; 5 use App \ Http \ Controllers \ Controller; 6 7 use Illuminate \ Http \ Request; 8 9 // reference the corresponding namespace 10 use Gregwar \ Captcha \ CaptchaBuilder; 11 use Session; 12 class CodeController extends Controller {13 public function captcha ($ temp) 14 {15 $ builder = new CaptchaBuilder ();
16 $ builder-> build (150,32 );
// Get the verification code 17 $ phrase = $ builder-> getPhrase ();
18 // Save the content to session19 Session: flash ('milkcaptcha ', $ phrase); // store the Verification Code 20Ob_clean (); // clear the cache21 return response ($ builder-> output ()-> header ('content-type', 'image/jpeg '); // output the verification code data in jpeg format 22} 23 24}
Then access the previously defined route in the browser and directly access this method to see the output verification code.
Method 2: display the verification code in the form and write the above file path to the src attribute of the label,
Shown as follows:
There are two problems to note: output the Verification Code directly according to the method below, you will find that the verification code is not displayed, but a bunch of garbled characters,
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 verification code content$ Phrase= $ Builder-> getPhrase (); // Save the content to sessionSession: flash ('milkcaptcha ', $ phrase); // generate an image header ("Cache-Control: no-cache, must-revalidate "); // clear the cache header ('content-Type: image/jpeg '); $ builder-> output ();}
Output$ Phrase,The verification code has been obtained, which is a problem in the Image Generation part,
// Header ("Cache-Control: no-cache, must-revalidate"); this line of code is used to clear the Cache to prevent the verification code from being refreshed or not displayed,
But it does not work. Use ob_clean (); To clear the browser cache.
Then
header('Content-Type: image/jpeg');$builder->output();
The question of these two statements,
$ Builder-> output ();
Only some information about the verification code image is returned. It is not an image. Therefore, when you output it directly, it is not an image,
Only write in this way
$ BuildGer-> output ()-> header ('content-type', 'image/jpeg ')
The verification code is output in the form of an image only when the image is output directly.
When you put it in the src attribute of , the label will automatically output it in the image format, that is
header('Content-type','image/jpeg')
This sentence is unnecessary at this time, so no matter
$buildGer->output())->header('Content-type','image/jpeg')
Write in this way, or
header('Content-Type: image/jpeg');$builder->output();
In this case, the verification code image is displayed.
Address: http://www.cnblogs.com/zbokett/p/7287235.html