Home > Article > Backend Development > How to generate verification code in laravel5.4
Summary: This blog introduces the specific steps of using gregwar/captcha to implement verification codes, as well as possible problems and solutions.
Operation steps:
1, find the composer.json file in the laravel5.4 project root directory,
2. Then open the command line, find the root directory of the project, run composer update,
you can see This 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 routing:
#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; //引用对应的命名空间 use Gregwar\Captcha\CaptchaBuilder; use Session; class CodeController extends Controller{ public function captcha($temp) { $builder = new CaptchaBuilder(); $builder->build(150,32); $phrase = $builder->getPhrase(); //把内容存入session Session::flash('milkcaptcha', $phrase); //存储验证码 ob_clean(); return response($builder->output())->header('Content-type','image/jpeg'); } }
In addition, you can also write like this in composer.json,
Or execute composer update in the project root directory, and then execute composer dump-autoload command.
The same effect can be achieved.
Finally, let me talk about the problems I have encountered. Many images of generating laravel verification codes on the Internet are written like this,
public function code($tmp) { //生成验证码图片的Builder对象,配置相应属性 $builder = new CaptchaBuilder; //可以设置图片宽高及字体 $builder->build($width = 100, $height = 40, $font = null); //获取验证码的内容 $phrase = $builder->getPhrase(); //把内容存入session Session::flash('milkcaptcha', $phrase); //生成图片 header("Cache-Control: no-cache, must-revalidate"); header('Content-Type: image/jpeg'); $builder->output(); }
I tried it, but the verification code picture showed garbled characters and no picture, as shown below:
Later I changed it and wrote it like this
public function captcha($temp) { $builder = new CaptchaBuilder(); $builder->build(150,32); $phrase = $builder->getPhrase(); //把内容存入session Session::flash('milkcaptcha', $phrase); //存储验证码 ob_clean(); return response($builder->output())->header('Content-type','image/jpeg'); }
can be displayed normally.
This article is reproduced at: http://www.cnblogs.com/zbokett/p/7287235.html
The above is the detailed content of How to generate verification code in laravel5.4. For more information, please follow other related articles on the PHP Chinese website!