1. Go to this page and download the library: Download mchev/banhammer 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/ */
mchev / banhammer example snippets
use Mchev\Banhammer\Traits\Bannable;
class User extends Model
{
use Bannable;
}
$user->ban();
use Mchev\Banhammer\Traits\Bannable;
class User extends Model
{
use Bannable;
}
// Get banned users
$bannedUsers = User::banned()->get();
// Get non-banned users
$activeUsers = User::notBanned()->get();
// Alternative syntax
$activeUsers = User::banned(false)->get();
// All bans for a model
$bans = $user->bans()->get();
// Only expired bans
$expired = $user->bans()->expired()->get();
// Active bans
$active = $user->bans()->notExpired()->get();
use Mchev\Banhammer\IP;
// Ban single IP
IP::ban("8.8.8.8");
// Ban multiple IPs
IP::ban(["8.8.8.8", "4.4.4.4", "1.1.1.1"]);
// Ban with expiration
IP::ban("8.8.8.8", [], now()->addMinutes(10));
// Ban with metadata
IP::ban("8.8.8.8", [
'reason' => 'spam',
'severity' => 'high'
]);
// Unban single IP
IP::unban("8.8.8.8");
// Unban multiple IPs
IP::unban(["8.8.8.8", "4.4.4.4"]);
// Check if IP is banned
IP::isBanned("8.8.8.8"); // bool
// Get all banned IPs
$ips = IP::banned()->get(); // Collection
$ips = IP::banned()->pluck('ip')->toArray(); // Array
'block_by_country' => true,
'blocked_countries' => ['FR', 'ES', 'US'],
// Single route
Route::get('/dashboard', [DashboardController::class, 'index'])
->middleware('auth.banned');
// Route group
Route::middleware(['auth.banned'])->group(function () {
Route::get('/profile', [ProfileController::class, 'index']);
Route::get('/settings', [SettingsController::class, 'index']);
});
// Block IPs on all routes
// Add to app/Http/Kernel.php:
protected $middleware = [
// ...
\Mchev\Banhammer\Middleware\IPBanned::class,
];
// config/ban.php
'scheduler_enabled' => false,
// config/ban.php
'scheduler_periodicity' => 'everyFiveMinutes', // or 'hourly', 'daily', etc.
use Mchev\Banhammer\Events\ModelWasBanned;
use Mchev\Banhammer\Events\ModelWasUnbanned;
Event::listen(ModelWasBanned::class, function ($event) {
// User was banned
Log::info("User {$event->ban->bannable->id} was banned");
});
Event::listen(ModelWasUnbanned::class, function ($event) {
// User was unbanned
Log::info("User {$event->ban->bannable->id} was unbanned");
});
// Set meta
$ban->setMeta('username', 'Jane');
$ban->setMeta('reason', 'spam');
// Get meta
$ban->getMeta('username'); // 'Jane'
// Check if meta exists
$ban->hasMeta('username'); // true
// Remove meta
$ban->forgetMeta('username');
// Find bans with specific meta
IP::banned()->whereMeta('username', 'Jane')->get();
$user->bans()->whereMeta('reason', 'spam')->get();
User::whereBansMeta('username', 'Jane')->get();
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Mchev\Banhammer\Models\Ban as BanhammerBan;
class Ban extends BanhammerBan
{
use HasUuids;
}