How to Install and Configure think-captcha in ThinkPHP
This tutorial walks through installing the think-captcha package via Composer, configuring its parameters, generating and displaying captchas in a ThinkPHP controller and view, setting up routing, and validating user input using both object and helper methods.
Step 1: Install the think-captcha package via Composer using composer require topthink/think-captcha .
Step 2: Configure the captcha parameters by editing config/captcha.php , for example setting 'length' => 4 and 'codeSet' => '0123456789' to generate a 4‑digit numeric code.
Step 3: Generate the captcha in a controller, e.g.:
<?php
namespace app\index\controller;
use think\captcha\facade\Captcha;
class Index{
public function verify(){
return Captcha::create();
}
}Step 4: Display the captcha in a view by adding {:captcha_img()} or <img src="{:captcha_src()}" alt="captcha" /> to the template.
Step 5: Access the URL http://YourUrl/index/index/verify to see the generated captcha.
Step 6: Define a route for the captcha, for example Route::get('captcha/[:config]', '\\think\\captcha\\CaptchaController@index'); .
Step 7: Verify the user input with either $captcha = new Captcha(); if(!$captcha->check($value)){ /* verification failed */ } or the helper if(!captcha_check($value)){ /* verification failed */ } .
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.