php-monsters/laravel-otp
Composer 安装命令:
composer require php-monsters/laravel-otp
包简介
Laravel OTP generator and validation
关键字:
README 文档
README
██████╗ ██╗ ██╗██████╗ ███╗ ███╗ ██████╗ ███╗ ██╗███████╗████████╗███████╗██████╗ ███████╗
██╔══██╗██║ ██║██╔══██╗ ████╗ ████║██╔═══██╗████╗ ██║██╔════╝╚══██╔══╝██╔════╝██╔══██╗██╔════╝
██████╔╝███████║██████╔╝ ██╔████╔██║██║ ██║██╔██╗ ██║███████╗ ██║ █████╗ ██████╔╝███████╗
██╔═══╝ ██╔══██║██╔═══╝ ██║╚██╔╝██║██║ ██║██║╚██╗██║╚════██║ ██║ ██╔══╝ ██╔══██╗╚════██║
██║ ██║ ██║██║ ██║ ╚═╝ ██║╚██████╔╝██║ ╚████║███████║ ██║ ███████╗██║ ██║███████║
╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝
https://github.com/php-monsters/
Laravel OTP
Introduction
A package for Laravel One Time Password (OTP) generator and validation without Eloquent Model, since it done by Cache. The cache connection same as your laravel cache config and it supported: "apc", "array", "database", "file", "memcached", "redis"
Installation
Install via composer
composer require php-monsters/laravel-otp
Configuration
Publish config and language file
php artisan vendor:publish --provider="PhpMonsters\Otp\OtpServiceProvider"
This package publishes an otp.php file inside your applications's config folder which contains the settings for this package.
Most of the variables are bound to environment variables, you may add Key-Value pair to the .env file in the Laravel application.
OTP_FORMAT=numeric
OTP_LENGTH=6
OTP_SENSITIVE=false
OTP_EXPIRES_TIME=15
OTP_ATTEMPT_TIMES=5
OTP_REPEATED=true
OTP_DEMO=false
Usage
Generate OTP
Otp::generate(string $identifier)
$identifier: The identity that will be tied to the OTP.
Sample
use OTP; // in your code $password = Otp::generate('samuraee@github.com');
This will generate a OTP that will be valid for 15 minutes.
Validate OTP
Otp::validate(string $identifier, string $password)
$identifier: The identity that is tied to the OTP.$password: The password tied to the identity.
Sample
use OTP; // in your code $result = Otp::validate('samuraee@github.com', '123456');
Responses
On Success
{
"status": true
}
Invalid OTP
{
"status": false,
"error": "invalid"
}
Expired
{
"status": false,
"error": "expired"
}
Max attempt
{
"status": false,
"error": "max_attempt"
}
- Reached the maximum allowed attempts, default 10 times with each identifier
Validate OTP by Laravel Validation
// in a `FormRequest` use PhpMonsters\Otp\Rules\OtpValidate; public function rules() { return [ 'code' => ['required', new OtpValidate('samuraee@github.com')] ]; } // in a controller $request->validate([ 'code' => ['required', new OtpValidate('samuraee@github.com')] ]);
Validate OTP by session id
// Otp class $result = Otp::validate('123456'); // in a `FormRequest` use PhpMonsters\Otp\Rules\OtpValidate; public function rules() { return [ 'code' => ['required', new OtpValidate()] ]; } // in a controller $request->validate([ 'code' => ['required', new OtpValidate()] ]);
- The setting without identifier will automatically use the session ID as the default, and the OTP generation and verification will be completed in same session (browser's cookies).
Advanced Usage
Generate OTP with options
$password = Otp::setLength(8)->setFormat('string')->setExpires(60)->setRepeated(false)->generate('identifier-key-here'); // or array option $password = Otp::generate('identifier-key-here', [ 'length' => 8, 'format' => 'string', 'expires' => 60, 'repeated' => false ]);
setLength($length): The length of the password. Default: 6setFormat($format): The format option allows you to decide which generator implementation to be used when generating new passwords. Options: 'string','numeric','numeric-no-zero','customize'. Default: "numeric"setExpires($minutes): The expiry time of the password in minutes. Default: 15setRepeated($boolean): The repeated of the password. The previous password is valid when new password generated until either one password used or itself expired. Default: true
Generate OTP with customize password
$password = Otp::setCustomize('12345678ABC@#$')->generate('identifier-key-here');
setCustomize($string): Random letter from the customize string
Validate OTP with specific attempt times
$password = Otp::setAttempts(3)->validate('identifier-key-here', 'password-here');
setAttempts($times): The number of incorrect password attempts. Default: 5
Validate OTP with case sensitive
$password = Otp::setSensitive(true)->generate('identifier-key-here'); // validate $result = Otp::setSensitive(true)->validate('identifier-key-here', 'password-here'); // in controller use PhpMonsters\Otp\Rules\OtpValidate; $request->validate([ 'code' => ['required', new OtpValidate('identifier-key-here', ['sensitive' => true])] ]);
setSensitive($boolean): Requiring correct input of uppercase and lowercase letters. Default: true
Generate OTP with seperate password
$password = Otp::setLength([4,3,4])->setSeparator(':')->generate('identifier-key-here');
Sample password
3526:126:3697
setLength($array): The length of the password, use array to separate each length.setSeparator($string): The separator of the password. Default: "-"
Validate OTP with extra data
$password = Otp::setData(['user_id' => auth()->id()])->generate('login-confirmation');
setData($var): Allows you to get the extra data of OTP.
// validate $result = Otp::setDisposable(false)->validate('login-confirmation', 'password-here'); // in controller use PhpMonsters\Otp\Rules\OtpValidate; $request->validate([ 'code' => ['required', new OtpValidate('login-confirmation', ['disposable' => false])] ]);
setDisposable($boolean): The disposable of the Otp identifier, the different password is not valid when same identifier password used. Default: true
On Success Response
{
"status": true,
"data": [
"user_id": 10
]
}
- When you set disposable to
false, you are able support different password with different extra data for different user in the same identifier key of the OTP.
Validate OTP with skip using
// validate $result = Otp::setSkip(true)->validate('identifier-key-here', 'password-here'); // in controller use PhpMonsters\Otp\Rules\OtpValidate; $request->validate([ 'code' => ['required', new OtpValidate('identifier-key-here', ['skip' => true])] ]);
setSkip($boolean): Skip using the password when validate, which means you can reuse the password again. Default: false- When there is an error response to the form request, it will skip using the password, but remember to
OTP::validate(...)in controller.
Delete OTP
Otp::forget('identifier-key-here');
- Delete all password with this specific identifier
Delete specific password
Otp::forget('identifier-key-here', 'password-here');
Reset attempt times
Otp::resetAttempt('identifier-key-here');
Demo password
Add the following Key-Value pair to the .env file in the Laravel application.
OTP_DEMO=true
- Demo mode for development purposes, no need to use real password to validate.
- Default demo password: "1234", "123456", "12345678"
Contribution
All contributions are welcome! 😄
License
The MIT License (MIT).
If you enjoy this, please consider supporting me:
php-monsters/laravel-otp 适用场景与选型建议
php-monsters/laravel-otp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「otp」 「email validation」 「One Time Password」 「mobile otp」 「one time pin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-monsters/laravel-otp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-monsters/laravel-otp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-monsters/laravel-otp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Extensible library for building notifications and sending them via different delivery channels.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
OTP generating package
Laravel contact us form package to send email and save to database
OTP generating package
Alfabank REST API integration
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-15