PHPackages                             zhuger/think-captcha - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. zhuger/think-captcha

ActiveLibrary

zhuger/think-captcha
====================

captcha package for thinkphp

v3.0.4(4y ago)08Apache-2.0PHP

Since Feb 17Pushed 4y ago1 watchersCompare

[ Source](https://github.com/zhuger/think-captcha)[ Packagist](https://packagist.org/packages/zhuger/think-captcha)[ RSS](/packages/zhuger-think-captcha/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

thinkphp6-captcha
=================

[](#thinkphp6-captcha)

thinkphp6 验证码类库,支持base64

安装
--

[](#安装)

> composer require zhuger/thinkphp6-captcha

前后端分离中使用
--------

[](#前后端分离中使用)

###### 生成短信验证码

[](#生成短信验证码)

```

use think\captcha\facade\CaptchaApi;

$data = CaptchaApi::createSMS($phone);

其中$data 返回为：
{
        "key": "13800138000",
		"code": "20"
    }

	phone: 为手机号
	key: 返回给前端，用于前端提交验证
	code: 验证码值，用短信发送出去

```

###### 验证短信验证码

[](#验证短信验证码)

```
CaptchaApi::checkSMS($code,$key);

CaptchaApi::checkSMS($code,$key,true); //强制删除缓存，用于验证次数限制

其中code为用户输入的短信验证码， key为上面返回到key 用于替代session， 同时使用cache作为缓冲。

```

###### 生成图片验证码

[](#生成图片验证码)

```

use think\captcha\facade\CaptchaApi;

$data = CaptchaApi::create();

其中$data 返回为：
{
        "base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoAAAA+CAMAAAA1S/atAAAAUVBMVEXz+/5PIIu7wNCXyJjEr5mY\r\noKKqrMbcyc+wnp3W0brRm5zLysbKxOGMcrZjO5lfNpC1qNLFurRwTJZ4Vqfe3++hjcSSeKKBYpys\r\nn7duSpl/TpGMCFihAAAACXBIWXMAAA7EAAAOxAGVKw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"$2y$10$JS3S//sVv5YhKZmxFdh4WevMPF3mhV0YpNr76daQs3acpv/JOUUIW",
        "md5": "c16a5320fa475530d9583c34fd356ef5",
		"code": "20"
    }

	base64: 图片base64地址
	key: 用于前端提交验证
	md5: 验证码端md5值，用于前端自我验证先，验证通过再请求发送 验证码和key到后台进行验证
	code: 验证码值，方便用于postman测试和自动化测试，切记正式上线，记得返回api是进行unset() 掉该值，避免被别人利用

```

###### 验证图片验证码

[](#验证图片验证码)

```
CaptchaApi::check($code,$key);

其中code为用户输入的验证码， key为上面返回到key 用于替代session， 同时使用cache作为缓冲

```

来源
--

[](#来源)

```
基于think-captcha进行扩展，保留了think-captcha所有功能，用于非前后端分离项目

```

使用
--

[](#使用)

### 在控制器中输出验证码

[](#在控制器中输出验证码)

在控制器的操作方法中使用

```
public function captcha($id = '')
{
	return captcha($id);
}

```

然后注册对应的路由来输出验证码

### 模板里输出验证码

[](#模板里输出验证码)

首先要在你应用的路由定义文件中，注册一个验证码路由规则。

```
\think\facade\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");

```

然后就可以在模板文件中使用

```
{:captcha_img()}

```

或者

```

```

> 上面两种的最终效果是一样的

### 控制器里验证

[](#控制器里验证)

使用TP的内置验证功能即可

```
$this->validate($data,[
    'captcha|验证码'=>'require|captcha'
]);

```

或者手动验证

```
if(!captcha_check($captcha)){
 //验证失败
};

```

###  Health Score

22

—

LowBetter than 25% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1494d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8719f19d1749d7059f5e2e4620874e099361eafb07d61b6b891c24e09b5cd522?d=identicon)[zhuger](/maintainers/zhuger)

---

Top Contributors

[![zhuger](https://avatars.githubusercontent.com/u/33123383?v=4)](https://github.com/zhuger "zhuger (7 commits)")

### Embed Badge

![Health badge](/badges/zhuger-think-captcha/health.svg)

```
[![Health](https://phpackages.com/badges/zhuger-think-captcha/health.svg)](https://phpackages.com/packages/zhuger-think-captcha)
```

###  Alternatives

[topthink/think

the new thinkphp framework

8.0k1.2M12](/packages/topthink-think)[topthink/think-queue

The ThinkPHP6 Queue Package

641664.0k66](/packages/topthink-think-queue)[topthink/think-swoole

Swoole extend for thinkphp

504170.5k18](/packages/topthink-think-swoole)[topthink/think-captcha

captcha package for thinkphp

132925.4k61](/packages/topthink-think-captcha)[topthink/think-worker

workerman extend for thinkphp

202224.6k9](/packages/topthink-think-worker)[topthink/think-migration

96452.1k117](/packages/topthink-think-migration)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
