Skip to content

Instantly share code, notes, and snippets.

@szepeviktor
Created November 11, 2025 08:14
Show Gist options
  • Select an option

  • Save szepeviktor/09e69861987226e8337d77b0f5d5e561 to your computer and use it in GitHub Desktop.

Select an option

Save szepeviktor/09e69861987226e8337d77b0f5d5e561 to your computer and use it in GitHub Desktop.
Detect HTTP requests from humans
#!/usr/bin/env php
<?php
#$ composer require matomo/device-detector
require __DIR__.'/vendor/autoload.php';
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);
while (!feof(STDIN)) {
$ua = trim(fgets(STDIN));
if ($ua === '') continue;
$dd = new DeviceDetector($ua);
$dd->parse();
$isHuman = !$dd->isBot()
&& ($dd->getDeviceName() === 'desktop' || $dd->isSmartphone() || $dd->isFeaturePhone() || $dd->isTablet() || $dd->isPhablet())
&& ($dd->isBrowser() || $dd->isMobileApp());
if ($isHuman) {
echo $ua, PHP_EOL;
}
}
@szepeviktor
Copy link
Author

Usage: cut -d'"' -f6 access.log | php is-human.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment