GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - Setono/BotDetectionBundle: Easily detect bots in your Symfony application

BranchesTags
Open more actions menu

Latest commit

 

History

125 Commits
125 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
bin
bin
 
 
src
src
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
ecs.php
ecs.php
 
 
infection.json.dist
infection.json.dist
 
 
phpstan.neon.dist
phpstan.neon.dist
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 

Repository files navigation

Symfony Bot Detection Bundle

Latest Version Software License Build Status Code Coverage Mutation testing

Detect if the user agent is a bot and act upon it. The detection is based on the bot list from matomo-org/device-detector, which is compiled into a single regular expression and updated weekly.

Before the full bot list is used, the user agent is checked against a short list of the most active crawlers (search engines, AI crawlers, SEO tools and social media fetchers). This makes it much faster to detect the most common bots and hence speeds up your request cycle.

Requirements

  • PHP 8.1 or higher
  • Symfony 6.4, 7.4 or 8

Installation

composer require setono/bot-detection-bundle

If you use Symfony Flex, the bundle is enabled automatically. Otherwise, add it to config/bundles.php:

<?php

return [
    // ...
    Setono\BotDetectionBundle\SetonoBotDetectionBundle::class => ['all' => true],
];

Usage

In your services

Inject BotDetectorInterface:

<?php

use Setono\BotDetectionBundle\BotDetector\BotDetectorInterface;

final class YourService
{
    public function __construct(private readonly BotDetectorInterface $botDetector)
    {
    }

    public function yourAction(string $userAgent): void
    {
        // Checks the user agent of the current main request
        if ($this->botDetector->isBotRequest()) {
            // do something to this bot!
        }

        // Checks a user agent string
        if ($this->botDetector->isBot($userAgent)) {
            // ...
        }
    }
}

isBotRequest() also accepts a Request object. It returns false if there is no request or the request has no User-Agent header.

In Twig templates

{% if is_bot_request() %}
    I knew you were a bot!
{% endif %}

{% if is_bot(user_agent) %}
    This user agent is a bot
{% endif %}

Configuration

The list of the most active crawlers is the setono_bot_detection.popular_bots parameter. You can override it in config/services.yaml, e.g. to put the bots that visit your site the most first:

parameters:
    setono_bot_detection.popular_bots:
        - Googlebot
        - bingbot
        - MyCustomCrawler

Each entry is a regular expression that is matched case-insensitively against the user agent. A user agent that matches an entry is treated as a bot, so keep the entries specific. A user agent that doesn't match any of them is still checked against the full bot list.

About

Easily detect bots in your Symfony application

Resources

Readme
MIT license
Activity
Custom properties

Stars

8 stars

Watchers

1 watching

Forks

3 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.