Example analysis of Laravel framework verification code class usage

  • 2021-12-21 04:19:42
  • OfStack

This paper illustrates the usage of Laravel framework verification code class. Share it for your reference, as follows:

In Laravel, there are many libraries of picture verification codes that can be used. This article introduces one of them: gregwar/captcha, which is relatively simple and commonly used in Laravel. Let's introduce the following usage details:

First, add the following configuration in composer. json:


"require": {
...
"gregwar/captcha": "1.*"
},

Then, the customary command:


composer update

Then it can be used normally. According to the specific development needs, there are many ways to use it.

You can save the verification code picture to a file:


<?php
$builder->save('out.jpg');

You can output pictures directly to the web page:


header('Content-type: image/jpeg');
$builder->output();

You can generate inline pictures:


<img src="<?php echo $builder->inline(); ?>" />

The following demonstrates one of the ways to output pictures directly to web pages.

I defined an Controller:


<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
// Reference the corresponding namespace 
use Gregwar\Captcha\CaptchaBuilder;
use Session;
class KitController extends Controller {
  /**
   * Display a listing of the resource.
   *
   * @return Response
   */
  public function captcha($tmp)
  {
    // That generates the verification code picture Builder Object, configure the corresponding properties 
    $builder = new CaptchaBuilder;
    // You can set the width, height and font of the picture 
    $builder->build($width = 100, $height = 40, $font = null);
    // Get the content of the verification code 
    $phrase = $builder->getPhrase();
    // Save the contents in session
    Session::flash('milkcaptcha', $phrase);
    // Generate a picture 
    header("Cache-Control: no-cache, must-revalidate");
    header('Content-Type: image/jpeg');
    $builder->output();
  }
}

Next, we can set the corresponding router to access this verification code picture and modify router. php:


Route::get('kit/captcha/{tmp}', 'KitController@captcha');

Now you can access this picture through the specific url

Verification code

The inside of the form is relatively simple, just look at it:


<input type="text" name="captcha" class="form-control" style="width: 300px;">
<a onclick="javascript:re_captcha();" ><img src="{{ URL('kit/captcha/1') }}" alt=" Verification code " title=" Refresh picture " width="100" height="40" id="c2c98f0de5a04167a9e427d883690ff6" border="0"></a>
<script>
 function re_captcha() {
  $url = "{{ URL('kit/captcha') }}";
    $url = $url + "/" + Math.random();
    document.getElementById('c2c98f0de5a04167a9e427d883690ff6').src=$url;
 }
</script>

Finally, verify the corresponding verification code on the form submission page, and the library also provides us with corresponding methods:


$userInput = $request->get('captcha');
if($builder->testPhrase($userInput)) {
  // The verification code entered by the user is correct 
  return ' You entered the verification code correctly ';
} else {
  // The user entered the verification code incorrectly 
  return ' You entered the verification code incorrectly ';
}

At this point, the verification code is completed. If you have any questions, please reply and discuss.

Supplement

The code written in form form submission verification is rather hasty, which brings ambiguity to readers. Here is a supplement (thanks for a yellow cloth reply):

$builder->testPhrase($userInput) Here, $builder is the same as $builder that generated the verification code. If new is re-used, 1 will fail to verify. We can see from the source code:


public function testPhrase($phrase)
{
 return ($this->builder->niceize($phrase) == $this->builder->niceize($this->getPhrase()));
}

The correctness judgment of the verification code can also be verified in the following ways:


composer update

0

For more readers interested in Laravel related content, please check the topics on this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

Hope that this article is based on the framework of Laravel PHP programming help.


Related articles: