---
title: "Block Bad Bots and Scrapers in Joomla - Code Snippets"
description: "Your analytics show traffic spikes that never convert, your server load climbs at odd hours, and the access log is full of crawlers you have never hea"
url: "https://www.tassos.gr/docs/code-snippets/howto/access-control-security/block-bad-bots"
date: "2026-08-24T00:16:07+00:00"
language: "en-GB"
---

[ Home ](https://www.tassos.gr/index.php?option=com_content&view=category&layout=blog&id=24&Itemid=1088) / [ Code Snippets ](https://www.tassos.gr/index.php?option=com_content&view=category&id=120) / [ How-to Guides ](https://www.tassos.gr/index.php?option=com_content&view=category&id=125) / [ Access Control &amp; Security ](https://www.tassos.gr/index.php?option=com_content&view=category&id=144)

#  Block Bad Bots and Scrapers in Joomla

Your analytics show traffic spikes that never convert, your server load climbs at odd hours, and the access log is full of crawlers you have never heard of. Aggressive scrapers and AI training bots ignore robots.txt, so asking politely does nothing. Blocking bad bots in Joomla by their user agent stops them at the door, and you don't have to install a full firewall extension to do it. You can do it with Code Snippets using a simple PHP snippet.

## [The PHP Snippet Code](#the-php-snippet-code)

Set **Code Type** to `PHP`, then use this snippet. On every frontend page load it looks at the visitor's user agent string and compares it to a list of fragments you define. Matching is case-insensitive and matches anywhere in the string, so `ahrefsbot` catches every version of that crawler. Blocked bots receive a `403 Forbidden` response before Joomla renders anything, which saves the database queries the page would have cost.

 ```
use Joomla\CMS\Factory;

// Any user agent containing one of these fragments will be blocked.
$blockedAgents = [
    'ahrefsbot',
    'semrushbot',
    'mj12bot',
    'dotbot',
    'petalbot',
    'gptbot',
    'ccbot',
];

// Message shown to blocked bots.
$message = 'Access denied.';

// Do not edit below.

$app       = Factory::getApplication();
$userAgent = strtolower((string) $app→getInput()→server→getString('HTTP_USER_AGENT', ''));

if ($userAgent !== '')
{
    foreach ($blockedAgents as $blockedAgent)
    {
        $blockedAgent = strtolower(trim($blockedAgent));

        if ($blockedAgent === '' || strpos($userAgent, $blockedAgent) === false)
        {
            continue;
        }

        $app→setHeader('Status', '403 Forbidden', true);
        $app→sendHeaders();

        echo $message;

        $app→close();
    }
}
```

**Tip:** Keep each fragment specific. Short strings like `bot` or `spider` match Googlebot and Bingbot too, and blocking those removes your site from search results. Never add `googlebot`, `bingbot`, or `duckduckbot` to this list unless you are certain that is what you want.

An empty user agent is left alone on purpose, because some legitimate clients and monitoring tools send none. Add a check for it only if your logs show it being abused.

## [How to add this snippet](#how-to-add-this-snippet)

1. Install **Code Snippets** if it is not installed already.
2. Go to your Joomla Administrator area.
3. Open **Components** → **Code Snippets**.
4. Click **New**.
5. Select **PHP** as the snippet type.
6. Paste the code to block bad bots above and adjust the list to the crawlers you see in your own logs.
7. Select **Insertion Method** → **Page Load**.
8. Publish the snippet.
9. Optionally, open the **Conditional Logic** tab to restrict which pages or users the snippet runs on. Leave it empty to run it site-wide. Learn more in [Using Conditional Logic](https://www.tassos.gr/docs/code-snippets/functionality/conditional-logic).

Congrats! You've just added bot blocking functionality to Joomla without installing another plugin or adding unnecessary bloat to your site.

For instance, to learn more about how the PHP Snippet works, visit our documentation: <https://www.tassos.gr/docs/tassos-code-snippets/types/php>

 Last updated on Aug 13th 2026 11:08
