How to generate verification code in laravel5.4

炎欲天舞
Release: 2023-03-14 18:08:01
Original
1648 people have browsed it

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,

How to generate verification code in laravel5.4

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(&#39;milkcaptcha&#39;, $phrase); //存储验证码
        ob_clean();
        return response($builder->output())->header(&#39;Content-type&#39;,&#39;image/jpeg&#39;);
    }

}
Copy after login

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(&#39;milkcaptcha&#39;, $phrase);
//生成图片
header("Cache-Control: no-cache, must-revalidate");
header(&#39;Content-Type: image/jpeg&#39;);
$builder->output();
}
Copy after login

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(&#39;milkcaptcha&#39;, $phrase); //存储验证码
      ob_clean();
      return response($builder->output())->header(&#39;Content-type&#39;,&#39;image/jpeg&#39;);
    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template