PHP code example of ognyk / yii2-login-blocker

1. Go to this page and download the library: Download ognyk/yii2-login-blocker 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/ */

    

ognyk / yii2-login-blocker example snippets


    'components' => [
        'loginBlocker' => [
            'class' => '\ognyk\loginblocker\LoginBlocker'
        ]
    ]
    

    /* Check if user can login */
    \Yii::$app->loginBlocker->check();
    
    /* Increment counter when wrong login or password */
    \Yii::$app->loginBlocker->block();
    

    if (!\Yii::$app->loginBlocker->check()) {
        return ['error' => 'time_block'];
    }
    
    if ($model->login()) {
        // ... good action here
    } else {
        \Yii::$app->loginBlocker->block();
        
        return ['error' => 'wrong_credentials'];
    }
    

    'components' => [
        'loginBlocker' => [
            'class' => '\ognyk\loginblocker\LoginBlocker',
            'time' => 300,              // Time to block/ban user in seconds (default 300 sec)
            'wrong_login_number' => 3,  // Number of wrong attempts (default 3 times)
        ]
    ]
    

    'components' => [
        'loginBlocker' => [
            'class' => '\ognyk\loginblocker\LoginBlocker',
            'mail' => [
                'subject' => 'New subject with user IP {ip}',
                'content' => 'User IP {ip}<br>Date: {date}<b>{params}',
                'sender' => [
                    'name' => 'Cezar II',
                    'mail' => '[email protected]',
                ],
                'mails' => [
                    '[email protected]',
                    '[email protected]',
                    '[email protected]',
                ],
            ]
        ]
    ]
    

    $params = [
        'Username' => 'Cezar V',
        'Server' => 'torr-2378-45'
    ];
    
    \Yii::$app->loginBlocker->check($params)
    

    'components' => [
        'loginBlocker' => [
            'class' => '\ognyk\loginblocker\LoginBlocker',
            'database' => [
                'name' => 'Login_blocker',
                'columns' =>  [
                    'ip' => '{ip}',
                    'created_datetime' => '{date}',
                    'username' => '{params.Username}', // More parameters from point 3
                ]
            ]
        ]
    ]