PHP Classes

How to Use a PHP SPAM Filter Middleware Class to Block Spam Requests Using the Package LaraGuard IP: Block HTTP requests from spamming IP addresses

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-04-22 (4 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
laraguard-ip 1.0.0BSD License7Libraries, Security, PHP 7
Description 

Author

This package can block HTTP requests from spamming IP addresses.

It provides a Laravel service class that calls a middleware class that processes HTTP requests and determine if the requests should be blocked depending on the request client IP address.

Currently, it can:

- Approve requests from IP addresses in a white list

- Approve requests from IP addresses from countries in a white list

- Deny requests from IP addresses that are in spam list

Picture of Neeraj Saini
  Performance   Level  
Name: Neeraj Saini <contact>
Classes: 3 packages by
Country: India India
Innovation award
Innovation award
Nominee: 2x

Instructions

Please read this instructions document to learn how to configure this package and use it in Laravel applications to block requests from IP addresses used to spam your site.

Documentation

<p align="center">

<img src="laraguard-ip-logo.png" alt="laraguard-ip Logo" style="width: 100%; max-width: 800px;" />

</p> <h1>? Laraguard-IP</h1>

A powerful Laravel package to protect your application using IP-based, country-based, and spam-blacklist-based middleware. <p align="center"> <a href="https://packagist.org/packages/haxneeraj/laraguard-ip"><img src="https://img.shields.io/packagist/dt/haxneeraj/laraguard-ip" alt="Total Downloads"></a> <a href="https://packagist.org/packages/haxneeraj/laraguard-ip"><img src="https://img.shields.io/packagist/v/haxneeraj/laraguard-ip" alt="Latest Stable Version"></a> <a href="https://packagist.org/packages/haxneeraj/laraguard-ip"><img src="https://img.shields.io/packagist/l/haxneeraj/laraguard-ip" alt="License"></a> </p>

> Created with ?? by Neeraj Saini _(opens in new tab)_

? Features

  • ? IP Whitelist Only Mode
  • ? Country Whitelist Mode (GeoIP-based) (COMING SOON)
  • ? Spam Protection using blocklists (like AbuseIPDB)
  • ? IP compression & JSON-based caching
  • ?? Middleware-based access control
  • ?? Artisan command to fetch & cache spam IPs

? Installation

composer require haxneeraj/laraguard-ip

? Configuration

1. Add the Service Provider

Add the service provider to the config/app.php file (this step may be optional if you're using Laravel's package auto-discovery):

For Laravel 10 and below:

    'providers' => [
        // Other service providers...
        Haxneeraj\LaraguardIp\Providers\LaraguardIpServiceProvider::class,
    ],

For Laravel 11: Add the service provider to the bootstrap/providers.php file (this step may be optional if you're using Laravel's package auto-discovery):

    return [
        // Other service providers...
        Haxneeraj\LaraguardIp\Providers\LaraguardIpServiceProvider::class,
    ];

2. Publish the config file

php artisan vendor:publish --provider="Haxneeraj\LaraguardIp\Providers\LaraguardIpServiceProvider" --tag=config

This will create a config file at:

config/laraguard-ip.php

? Middleware Usage

The middleware is automatically registered via the package's service provider. You can directly use it in your routes like this:

Apply to Routes

Route::middleware(['laraguard-ip'])->group(function () {
    // Protected routes
});

?? Configuration Options

Here's a summary of available config values:

| Key | Description | Default | | ----------------------------- | ----------------------------------- | ------------------------------------------- | | laraguardip_spam_protection | Enable blocklist protection | false | | whitelist_only_access | Only allow IPs in whitelist_ips | false | | country_whitelist_only | Only allow from country_whitelist | false | | whitelist_ips | Safe IPs | ['127.0.0.1'] | | country_whitelist | Allowed ISO country codes | ['IN', 'US'] | | sources | Blocklist sources (URL) | AbuseIPDB | | path | Blocklist storage path | storage/framework/cache/laraguard-ip.json | | compress | Enable IP compression | true |

? Fetch & Cache Blacklist IPs

You can fetch and store the latest spam IPs from the defined sources:

php artisan laraguardip:update

  • Downloads IPs from `config('laraguard-ip.sources')`
  • Saves to `laraguard-ip.json`
  • Caches the list forever using Laravel's Cache

? How It Works

  • ? Denies access if IP is not whitelisted (if enabled)
  • ? Blocks requests from unwanted countries (if enabled)
  • ? Checks IPs against blacklist
  • ? Cache and config driven ? no DB required

? Example .env

LARAGUARDIP_SPAM_PROTECTION=true
LARAGUARDIP_WHITELIST_ONLY=false
LARAGUARDIP_COUNTRY_WHITELIST_ONLY=false
LARAGUARDIP_STORAGE_PATH=framework/cache/laraguard-ip.json
LARAGUARDIP_STORAGE_COMPRESS=true

? Tests

This package comes with feature and unit tests to ensure everything works smoothly.

? Run Tests

composer test

? Want to Contribute?

Pull requests are welcome! Feel free to fork, raise issues or drop ideas.

? License

MIT © Neeraj Saini _(opens in new tab)_


  Files folder image Files (11)  
File Role Description
Files folder imageconfig (1 file)
Files folder imagesrc (3 directories)
Files folder imagetests (1 directory)
Accessible without login Plain text file .phpunit.result.cache Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Image file laraguard-ip-logo.png Icon Icon image
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (11)  /  config  
File Role Description
  Accessible without login Plain text file laraguard-ip.php Conf. Configuration script

  Files folder image Files (11)  /  src  
File Role Description
Files folder imageConsole (1 directory)
Files folder imageHttp (1 directory)
Files folder imageProviders (1 file)

  Files folder image Files (11)  /  src  /  Console  
File Role Description
Files folder imageCommands (1 file)

  Files folder image Files (11)  /  src  /  Console  /  Commands  
File Role Description
  Plain text file LaraguardIpCommand.php Class Class source

  Files folder image Files (11)  /  src  /  Http  
File Role Description
Files folder imageMiddleware (1 file)

  Files folder image Files (11)  /  src  /  Http  /  Middleware  
File Role Description
  Plain text file LaraguardIpMiddleware.php Class Class source

  Files folder image Files (11)  /  src  /  Providers  
File Role Description
  Plain text file LaraguardIpServiceProvider.php Class Class source

  Files folder image Files (11)  /  tests  
File Role Description
Files folder imageFeature (2 files)

  Files folder image Files (11)  /  tests  /  Feature  
File Role Description
  Plain text file LaraguardIpCommandTest.php Class Class source
  Plain text file LaraguardIpMiddlewareTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0