Download the PHP package petalbranch/icon-captcha without Composer
On this page you can find all versions of the php package petalbranch/icon-captcha. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download petalbranch/icon-captcha
More information about petalbranch/icon-captcha
Files in petalbranch/icon-captcha
Package icon-captcha
Short Description IconCaptcha is a lightweight and secure PHP library for generating image-based click CAPTCHA challenges using open-source icon fonts (e.g., Font Awesome).
License Apache-2.0
Informations about the package icon-captcha
IconCaptcha
👉 中文 | English
这是一个轻量级、安全且易于集成的 PHP 图标点选验证码库。用户需要按照提示图标的顺序,在图片中依次点击对应的图标来完成验证。
✨ 特性
- 安全性:验证逻辑完全在服务端执行,坐标位置带有容错校验。
- 自定义:支持自定义验证码尺寸、图标数量、干扰项数量。
- 扩展性:支持自定义字体文件(实现 IconSetInterface)和背景图片文件夹。
- 高性能:惰性加载资源,低内存占用
👀 示意图
以下图片为前端的验证码展示效果参考,具体需要自行实现。
📦 安装
⚠️ 安全警告 (Security Warning)
icon_captcha_generate() 函数返回的数组中包含 answer 字段(正确坐标数据)。
绝对禁止 将 answer 字段直接返回给前端(浏览器/客户端)。
你 必须 将 answer 数据存储在服务端的 Session、Redis 或数据库中,验证时再取出比对。
快速开始
- 生成验证码
在你的控制器(Controller)中生成验证码数据:
- 前端展示说明 (客户端)
前端在接收到数据后,需要注意 主图 和 提示图标 的 Base64 前缀处理方式不同。
- 主图 (
image):可能是 WebP 或 PNG,需根据返回的 mime 字段动态拼接。 - 提示图标 (
icons):为了保持透明背景,统一固定为 PNG 格式。
- 主图 (
HTML/JS 拼接示例:
- 验证验证码 (服务端)
前端收集用户的点击坐标后,提交到验证接口:
🔐 安全增强:点击位置加密
为了防止恶意用户直接通过抓包工具篡改或伪造点击坐标,建议在前端对点击位置进行加密,后端解密后再传入 icon_captcha_verify 进行校验。
流程:
- 前端:用户点击 -> 获得坐标数组 -> JSON序列化 -> 加密 (AES/RSA) -> 发送密文。
- 后端:接收密文 -> 解密 -> 获得坐标数组 -> 调用
icon_captcha_verify。具体代码示例请参考项目 examples 或根据自身业务逻辑实现。
- 前端加密 (示例)
假设你使用 crypto-js 或其他库对坐标数组进行 AES 加密:
- 后端解密与校验
在 PHP 控制器中,先解密数据,还原成数组格式,再传给验证函数:
⚙️ 参数配置
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
$width |
int | 320 | 验证码图片宽度 |
$height |
int | 200 | 验证码图片高度 |
$length |
int | 4 | 需要用户点击的正确图标数量 |
$decoyIconCount |
int | 2 | 干扰图标数量(不计入答案) |
$iconSet |
IconSetInterface | null | 自定义字体集实例 |
$backgroundImageFolder |
string | null | 自定义背景图片文件夹路径 |
$useWebp |
bool | true | 优先使用 WebP 格式(体积更小),若不支持自动回退到 PNG |
🎨 高级用法
自定义背景图
默认情况下,库会生成随库自带的随机背景。如果你想使用随机背景图片:
自定义图标字体 默认的 IconSet 是配合内置的 FontAwesome 字体使用的。如果你想使用自己的 .ttf 字体文件,你需要创建一个新类来实现 IconSetInterface 接口,并定义该字体对应的 Unicode 映射表。
-
创建自定义 IconSet 类
- 在生成时使用
📄 许可证
本项目遵循 Apache License 2.0 开源协议。
All versions of icon-captcha with dependencies
ext-mbstring Version *