PHPackages                             barrett/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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. barrett/captcha

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

barrett/captcha
===============

captcha package

2.4(7y ago)141.3k5Apache-2.0PHP

Since May 11Pushed 7y ago2 watchersCompare

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

READMEChangelog (4)DependenciesVersions (15)Used By (0)

Barrett-captcha
===============

[](#barrett-captcha)

验证码扩展库

使用
--

[](#使用)

### 一、安装

[](#一安装)

> composer require barrett/captcha

### 二、初始化配置

[](#二初始化配置)

引入本扩展库后，在实例化时可以传递配置参数，用于处理不同的场景。

```
$Captcha = new barrett\Captcha(['setZh'=>true,'length'=>4]);
```

> 配置表如下

参数名参数类型默认值说明keystringBarrett验证码加密keydestroyBooleantrue验证成功后是否销毁expireinteger1800验证码过期时间（s）lengthinteger5验证码长度setZhBooleanfalse使用中文验证码widthinteger180图片验证码默认宽度heightinteger50图片验证码默认高度fontSizeinteger20验证码字体大小(px)fontTtfstring验证码字体，不设置随机获取useImgBgBooleanfalse使用背景图片useCurveBooleantrue是否画混淆曲线useNoiseBooleantrue是否添加杂点bgarray\[243, 251, 254\]背景颜色### 二、创建验证码

[](#二创建验证码)

本扩展目前支持：图形验证码、数字验证码（短信用）两种，使用方法如下：

> 创建图形验证码

```
$Captcha = new barrett\Captcha();
return $Captcha->createImg('user');
```

> 创建短信验证码

```
$Captcha = new barrett\Captcha();
//createNum方法会返回被创建的验证码
$code = $Captcha->createNum('user');
//使用短信发送数字验证码
$Sms->sendToPhone($code['data]);
```

> 验证码效验

```
$Captcha = new barrett\Captcha();
return $Captcha->check($_POST,'user');
```

\####如输出结果异常请检查页面头部信息是否正常！如：Content-type:image/png 是否设置

### 三、场景标识

[](#三场景标识)

用例：客户更改已绑定的手机号，需要在同一个页面生成两个验证码（原手机验证码、新手机验证码）。 1.原手机创建验证码加入唯一场景标识：

```
$old = $Captcha->createNum('old');
```

2.新手机创建验证码同样加入唯一场景标识：

```
$new = $Captcha->createNum('new');
```

3.当用户提交数据时则根据唯一场景标识来检测对应场景的验证码是否正确，而不会出现相同页面多次调用后不能效验的问题。

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Every ~69 days

Recently: every ~102 days

Total

14

Last Release

2749d ago

Major Versions

v1.0.7 → 2.0.x-dev2017-09-15

### Community

Maintainers

![](https://www.gravatar.com/avatar/d801f26cd94e116adcd1ef2e6969d45ac03613e0c041d49547c1261942103b8a?d=identicon)[Barrett-M82A1](/maintainers/Barrett-M82A1)

---

Top Contributors

[![yunwuxin](https://avatars.githubusercontent.com/u/2168125?v=4)](https://github.com/yunwuxin "yunwuxin (10 commits)")[![CyberLife-Markus](https://avatars.githubusercontent.com/u/27344136?v=4)](https://github.com/CyberLife-Markus "CyberLife-Markus (7 commits)")[![liu21st](https://avatars.githubusercontent.com/u/1111670?v=4)](https://github.com/liu21st "liu21st (4 commits)")[![zhaishuaigan](https://avatars.githubusercontent.com/u/6310602?v=4)](https://github.com/zhaishuaigan "zhaishuaigan (1 commits)")

### Embed Badge

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

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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