Verification Code library for Laravel

Source: Internet
Author: User
In LaravelThere are a lot of picture verification code in the library can be used, this chapter introduces its LaravelOne: Gregwar/captcha, this library is relatively simple, in Laravelare more commonly used. Below we will introduce the following details of use:

First, the configuration is added as follows in Composer.json:

"Require": {        ...        " Gregwar/captcha ":" 1.* "    },

Then, have become accustomed to the command:

Composer Update

The next step is to use it normally, depending on the development needs, there are many ways to use it.

You can save a captcha picture file:

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

You can output images directly to a Web page:

<?phpheader (' content-type:image/jpeg '); $builder->output ();

You can generate inline pictures:

inline ();?>"/>

The following shows one of the ways to use a direct output image to a Web page.

Define a controller:

<?php namespace App\http\controllers;use app\http\requests;use app\http\controllers\controller;use Illuminate\ Http\request;

referencing 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)   {       //Generate CAPTCHA Picture Builder object, configure corresponding properties       $ Builder = new Captchabuilder;       You can set the picture width height and font       $builder->build ($width =, $height = +, $font = null);       Get the contents of the captcha       $phrase = $builder->getphrase ();       Deposit the content       in Session session::flash (' Milkcaptcha ', $phrase);       Generate Picture       header ("Cache-control:no-cache, must-revalidate");       Header (' Content-type:image/jpeg ');       $builder->output ();   }}

Below we can set the corresponding router to access this CAPTCHA image, modify the router.php:

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

You can now see this image through the specific URL that you can access.

Verification Code

The inside of the form is relatively simple to read:

<input type= "text" name= "Captcha" class= "Form-control" style= "width:300px;" ><a onclick= "Javascript:re_captcha ();" >  </a><script>    function Re_captcha () {    $url = "{{URL (' Kit/captcha ')}}";        $url = $url + "/" + math.random ();        document.getElementById (' C2c98f0de5a04167a9e427d883690ff6 '). src= $url;  } </script>

Finally, in the form submission page to verify the corresponding verification code, the library also provides us with the corresponding method:

$userInput = $request->get (' Captcha '), if ($builder->testphrase ($userInput)) {    //user input Verification code correct    return ' You entered the correct code ";} else {    //user input code Error    return ' you entered a CAPTCHA error ';}

At this point, the verification code is complete. The above is laravel Verification Code Library Gregwar/captcha the use of the process, interested in topic.alibabacloud.com search more Oh ~

Related recommendations:

Laravel How to use MongoDB database tutorial

Development of SMS Verification code sending function in Laravel

Examples of use of model validation in Laravel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.