Table of Contents
Articles you may be interested in:
Home Backend Development PHP Tutorial Laravel5.2 uses Captcha to generate verification codes to achieve login

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

Jul 03, 2018 pm 05:57 PM
laravel session Generate verification code

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:

<img src="{{url(&#39;/captcha/1&#39;)}}" alt="" onclick="this.src=&#39;{{url(&#39;/captcha&#39;)}}/&#39;+Math.random()" width="100" height="40" border="0">
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(&#39;captcha&#39;);
  if (Session::get(&#39;milkcaptcha&#39;) == $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.

Summary

The above is the editor’s introduction to Laravel5.2 using Captcha to generate verification codes to achieve login (session pit) , I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the php Chinese website!

Articles you may be interested in:

Detailed explanation of bucket sorting in PHP sorting algorithm series

##PHP Sorting Algorithm Series Detailed Explanation of Merge Sort_php Skills

PHP Sorting Algorithm Series Detailed Explanation of Direct Selection Sorting

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

How to effectively check the validity of Redis connections in Laravel6 project? How to effectively check the validity of Redis connections in Laravel6 project? Apr 01, 2025 pm 02:00 PM

How to check the validity of Redis connections in Laravel6 projects is a common problem, especially when projects rely on Redis for business processing. The following is...

Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Apr 01, 2025 pm 12:21 PM

A problem of duplicate class definition during Laravel database migration occurs. When using the Laravel framework for database migration, developers may encounter "classes have been used...

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

See all articles