Detailed explanation of verification code generation using laravel5.4

巴扎黑
Release: 2023-03-15 08:36:01
Original
1653 people have browsed it

The following editor will bring you an example of how to generate verification codes in laravel5.4. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look

Summary: This article introduces the specific steps of using gregwar/captcha to implement verification codes, as well as possible problems and solutions.

Operation steps:

1, find composer.json in the laravel5.4 project root directory This file,

add

"gregwar/captcha": "1.*" to composer.json this file, as shown below.

2. Then open the command line, find the root directory of the project, run composer update,

##You can see that 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 the route:

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

Then access the previously defined route in the browser and you will see the verification code

In addition, you can also write like this in composer.json,

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

The same effect can be achieved.

Finally, let me talk about the problems I have encountered. Many pictures 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 will try it. I tried it, but the verification code picture showed garbled characters and no picture was displayed, as shown below:

Later it was changed


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.

The above is the detailed content of Detailed explanation of verification code generation using 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