PHP code example of webiik / attempts

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

    

webiik / attempts example snippets


// Instatiate Attempts
$attempts = new \Webiik\Attempts\Attempts();

// Set storage (you have to write your own storage)
$attempts->setStorage(function() {
    return new \Webiik\Attempts\YourCustomStorage();
});

// Store user login attempt
$attempts->write('login', $attempts->getIp());

// Get user login attempts within the last hour
$startTimestamp = time() - 60 * 60;
$loginAttempts = $attempts->readByIp('login', $attempts->getIp(), $startTimestamp);

if(count($loginAttempts) > 10) {
    // E.g. Temporary prevent user to log in
}

// Delete expired login attempts with probability 2/100
$attempts->delete('login', $startTimestamp, 2);

setStorage(callable $factory): void

$attempts->setStorage(function() {
    return new \Webiik\Attempts\YourCustomStorage();
});

getIp(): string

$ip = $attempts->getIp();

write(string $label, string $ip, string $hash = ''): void

$attempts->write('login', $ip, $hash);

read(string $label, string $ip, string $hash, int $startTimestamp = 0): array

$startTimestamp = time() - 60 * 60;
$loginAttempts = $attempts->read('login', $ip, $hash, $startTimestamp);

readByIp(string $label, string $ip, int $startTimestamp = 0): array

$startTimestamp = time() - 60 * 60;
$loginAttempts = $attempts->readByIp('login', $ip, $startTimestamp);

readByHash(string $label, string $hash, int $startTimestamp = 0): array

$startTimestamp = time() - 60 * 60;
$loginAttempts = $attempts->readByHash('login', $hash, $startTimestamp);

delete(string $label, int $timestamp, int $probability = 1): void

$olderThanTimestamp = time() - 60 * 60;
$attempts->delete('login', $olderThanTimestamp);

deleteAll(int $timestamp, int $probability = 1): void

$olderThanTimestamp = time() - 60 * 60;
$attempts->delete($olderThanTimestamp);