Laravel5.2 uses Captcha to generate verification codes to implement login

不言
Release: 2023-03-31 22:06:02
Original
1690 people have browsed it

This article mainly introduces Laravel5.2's use of Captcha to generate verification codes (session pit). Friends who need it can refer to it

Recently, a friend asked me to help with laravel's verification code login, so Did a little research. (I have almost forgotten how to use laravel)

First of all, there is no need to go into details about installing laravel. My version is 5.2.45 (note: middleware for versions above laravel 5.2.6 can be loaded automatically) , this is still quite important.

After the installation is complete, you need to use composer to load your Captcha. The specific method is to add the line of code "gregwar/captcha":"1.*" to the require array in your composer.json . Then, use cmd in the root directory of your project to run the composer update line of code. In this case, even if the library or middleware is installed. Then you can write your code freely.

php: (I won’t go into too much details about the specific routing below, just write the key code)

public function captcha($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(); }
Copy after login

blade template Call:

Copy after login

If you think this is the end of it, then you are really too yang too simple. When you verify, you will find that everything is wrong.

Isn’t it so sour? Because in laravel5.2, all sessions cannot cross controllers and methods. If they cross, the session will be regenerated. By default, the session must be passed through middleware. Don't worry, the next method is the solution. Currently, there are two solutions that I know of. One is to build a middleware yourself, and then store all sessions in it, but it is a little troublesome. Next, I will introduce a simple method, in your laravel\app Add the following code to $middleware in the \Http\Kernel.php file:

\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Cookie\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class,
Copy after login

Then you can use the session with confidence. This is the way to verify the verification code below.

public function login_data() { $userInput = \Request::get('captcha'); if (Session::get('milkcaptcha') == $userInput) { //用户输入验证码正确,验证你自己密码用户名 echo 1; } else { //用户输入验证码错误 echo 2; } }
Copy after login

Finally, I have to complain about laravel’s official documentation. Such pitfalls are not explained in advance, I guess. I'm worried that the developers are too happy with the development.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the use of date and time processing package Carbon in Laravel

About php artisan migrate under laravel 5.1 Use

How to solve the problem that the log cannot be written in Laravel

##

The above is the detailed content of Laravel5.2 uses Captcha to generate verification codes to implement login. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!