jaybizzle/crawler-detect
PHP library to detect bots, crawlers, and spiders by inspecting User-Agent and HTTP_FROM headers. Recognizes thousands of user agents, updated regularly. Simple API: isCrawler() for current or given UA, and getMatches() to see the detected bot name.
CrawlerDetect) with no external dependencies beyond PHP, making it easy to integrate into Laravel without bloating the stack.src/Fixtures/Crawlers.php), allowing customization via regex patterns without forking the package.isCrawler() in middleware to block/rate-limit bots at the HTTP layer.Jaybizzle\CrawlerDetect\CrawlerDetect as a singleton for dependency injection.Request class with a isCrawler() helper method.Request object provides $request->userAgent() and $request->header('HTTP_FROM'), aligning perfectly with CrawlerDetect’s requirements.tests/data/user_agent/crawlers.txt), enabling unit tests for edge cases (e.g., false positives).getMatches() to log unidentified UAs for manual review and contribute updates to the package.User-Agent and HTTP_FROM; spoofable headers could bypass detection. Combine with IP-based checks (e.g., spatie/fake-useragent-laravel) for defense-in-depth.isCrawler() in your Laravel app? Benchmark with your traffic patterns.bot/lite, spatie/fake-useragent-laravel) or supplement it?namespace App\Http\Middleware;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
use Closure;
class DetectCrawlers
{
public function __construct(private CrawlerDetect $detector) {}
public function handle($request, Closure $next)
{
if ($this->detector->isCrawler()) {
// Block, log, or redirect bots
return response('Forbidden', 403);
}
return $next($request);
}
}
$this->app->singleton(CrawlerDetect::class, function ($app) {
return new CrawlerDetect();
});
use Jaybizzle\CrawlerDetect\CrawlerDetect;
Request::macro('isCrawler', function () {
return app(CrawlerDetect::class)->isCrawler();
});
spatie/laravel-package-tools for compatibility).isCrawler() in a single route/middleware.Illuminate\Support\Facades\Cache (e.g., 5-minute TTL for IP + User-Agent hashes).Request object. Example:
$detector = new CrawlerDetect();
$detector->setUserAgent($request->userAgent());
$detector->setHttpFrom($request->header('HTTP_FROM'));
bot/lite).spatie/analytics to exclude bots from event tracking.composer.json allows jaybizzle/crawler-detect (no conflicts with other jaybizzle packages).composer require jaybizzle/crawler-detect
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(CrawlerDetect::class);
}
curl -A "Googlebot").src/Fixtures/Crawlers.php or extend the class. Example:
class CustomCrawlerDetect extends CrawlerDetect
{
protected function getCrawlerData(): array
{
$data = parent::getCrawlerData();
$data['custom_bot'] = '/CustomBot\//i';
return $data;
}
}
getMatches() to identify misclassified UAs and submit fixes upstream.How can I help you explore Laravel packages today?