PHP code example of codemonster-ru / security
1. Go to this page and download the library: Download codemonster-ru/security library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codemonster-ru / security example snippets
namespace App\Providers;
use Codemonster\Annabel\Providers\SecurityServiceProvider as BaseSecurityServiceProvider;
class SecurityServiceProvider extends BaseSecurityServiceProvider {}
return [
'csrf' => [
'enabled' => true,
'add_to_kernel' => true,
'verify_json' => false,
'input_key' => '_token',
'except_methods' => ['GET', 'HEAD', 'OPTIONS'],
'except' => ['api/*'],
],
'throttle' => [
'enabled' => true,
'add_to_kernel' => false,
'max_attempts' => 60,
'decay_seconds' => 60,
'storage' => 'session', // session | database | redis
'connection' => null, // database connection name
'table' => 'throttle_requests',
'redis' => null, // Redis client instance or container id/class
'prefix' => 'throttle:',
'presets' => [
'login' => [
'ip' => ['max_attempts' => 60, 'decay_seconds' => 60],
'account' => [
'max_attempts' => 5,
'decay_seconds' => 60,
'field' => 'email',
],
],
'api' => ['max_attempts' => 120, 'decay_seconds' => 60],
],
'except' => [],
'trusted_proxies' => ['10.0.0.0/8'],
],
];
echo '<form method="POST" action="/submit">';
echo csrf_field();
echo '<button type="submit">OK</button>';
echo '</form>';
'throttle' => [
'storage' => 'database',
'connection' => null,
'table' => 'throttle_requests',
],
// config/database.php
return [
'migrations' => [
'paths' => [
base_path('database/migrations'),
base_path('vendor/codemonster-ru/security/migrations'),
],
],
];
'throttle' => [
'storage' => 'database',
'table' => 'app_rate_limits',
],
'throttle' => [
'storage' => 'redis',
'redis' => Redis::class, // container id/class or instance
'prefix' => 'throttle:',
],
use Codemonster\Security\RateLimiting\ThrottleRequests;
$app->post('/login', fn($req) => 'ok')
->middleware(ThrottleRequests::class, '5,60'); // 5 attempts in 60 seconds
$app->post('/login', fn($req) => 'ok')
->middleware(ThrottleRequests::class, 'login');
use Codemonster\Security\RateLimiting\ThrottleRequests;
$app->post('/login', fn($req) => 'ok')
->middleware(ThrottleRequests::class, function ($req) {
return 'login:' . ($req->input('email') ?? 'guest') . '|' . $req->ip();
});