PHP code example of rmanara / brutalforce

1. Go to this page and download the library: Download rmanara/brutalforce 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/ */

    

rmanara / brutalforce example snippets




rutalForce\Initiate;

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$guard = new Initiate();

// Text label based on current cadence profile
$rate = $guard->Rate();

// Numeric score (rounded to 3 decimals)
$score = $guard->predict();

if ($score <= 0.5) {
    http_response_code(429);
    exit('Too many requests');
}

$decision = $guard->decision();

if ($decision['blocked']) {
    header('Retry-After: ' . $decision['retry_after']);
    http_response_code(429);
    exit('Too many requests');
}

$guard = new Initiate([
    'threshold' => 0.5,
    'violation_limit' => 5,
    'lock_steps' => [60, 120, 300],
]);

$guard = new Initiate(
    ['threshold' => 0.5],
    [
        // Session key namespacing/versioning
        'session_namespace' => 'brutalforce',
        'session_version' => 'v1',

        // Trusted proxy strategy
        'trusted_proxies' => ['127.0.0.1'],
        'forwarded_headers' => ['HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP'],

        // Optional distributed backend (phpredis client instance)
        // 'redis' => $redis,
        // 'redis_prefix' => 'brutalforce:v1',

        // Optional deterministic/testing clock (ClockInterface)
        // 'clock' => $clock,
    ]
);

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$guard = Initiate::withRedis(
    $redis,
    ['threshold' => 0.5, 'violation_limit' => 5, 'lock_steps' => [60, 120, 300]],
    ['redis_prefix' => 'brutalforce:v1']
);
bash
php scripts/simulate.php --scenario=delay-2s
php scripts/simulate.php --format=json
bash
composer simulate:redis
php scripts/simulate_redis.php --format=json