Banhammer : Block Users, Model and IPs in Laravel

Banhammer : Block Users, Model and IPs in Laravel

Banhammer is a Laravel package that provides a simple and easy-to-use way to manage user bans and IP bans in your Laravel application.

Banhammer is a Laravel package that provides a simple and easy-to-use way to manage user bans and IP bans in your Laravel application. The package provides a flexible and customizable system that allows you to ban users and IP addresses based on a variety of criteria.

With Banhammer, you can specify the type of ban (user or IP), the duration of the ban, and the reason for the ban. You can also customize the ban message that is displayed to the user when they attempt to access your application while banned.

The package includes a middleware that checks for banned users and IP addresses on each request, ensuring that banned users and IP addresses are prevented from accessing your application. The middleware also provides options for customizing the response that is sent to banned users, such as redirecting them to a specific page or displaying a custom message.

Banhammer is a Laravel package that provides a simple and flexible way to ban users and IPs from your application. Here are the steps to use Banhammer in your Laravel app:

  1. Install Banhammer using Composer:
composer require lightit/banhammer
  1. Publish the configuration file:
php artisan vendor:publish --provider="Lightit\Banhammer\BanhammerServiceProvider" --tag="config"
  1. Migrate the Banhammer database tables:
php artisan migrate
  1. In your user model, add the Bannable trait:
use Illuminate\Foundation\Auth\User as Authenticatable;
use Lightit\Banhammer\Traits\Bannable;

class User extends Authenticatable
{
    use Bannable;

    // ...
}
  1. Ban a user or IP:
// Ban a user by ID
$user = User::find(1);
$user->ban();

// Ban an IP address
Banhammer::ban('127.0.0.1');
  1. Check if a user or IP is banned:
// Check if a user is banned
$user = User::find(1);
if ($user->isBanned()) {
    // Do something with the banned user
}

// Check if an IP is banned
if (Banhammer::isBanned('127.0.0.1')) {
    // Do something with the banned IP
}
  1. Unban a user or IP:
// Unban a user by ID
$user = User::find(1);
$user->unban();

// Unban an IP address
Banhammer::unban('127.0.0.1');

That's it! With Banhammer, you can easily ban users and IPs from your Laravel application.

Did you find this article valuable?

Support Laravel Tips & Tutorials - KeyTech Blog by becoming a sponsor. Any amount is appreciated!