Skip to content

Instantly share code, notes, and snippets.

@asheroto
Last active April 24, 2023 11:00
Show Gist options
  • Select an option

  • Save asheroto/ea60068d947967eb44475755b60d99a6 to your computer and use it in GitHub Desktop.

Select an option

Save asheroto/ea60068d947967eb44475755b60d99a6 to your computer and use it in GitHub Desktop.
Using PHP, detect if a visitor browser based or command line based connection. Does not differentiate between desktop and mobile browsers. Not a definitive list of user agents.
<?php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$browsers = array(
'Firefox', 'Chrome', 'Safari', 'Opera', 'Internet Explorer', 'Edge', 'Android Browser', 'Samsung Internet', 'UC Browser', 'Yandex Browser', 'Brave', 'Vivaldi', 'Maxthon', 'SeaMonkey', 'Pale Moon', 'Mozilla', 'Netscape', 'Konqueror', 'Epiphany', 'Midori', 'Links', 'Lynx', 'w3m', 'Dillo', 'iCab', 'OmniWeb', 'Camino', 'Shiira', 'Flock', 'Galeon', 'K-Meleon', 'SlimBrowser', 'GreenBrowser', 'Avant Browser', 'Iron', 'RockMelt', 'Comodo Dragon', 'Coowon', 'Sleipnir', 'Sputnik Browser', 'Maxthon Nitro', 'Cyberfox', 'Waterfox', 'Basilisk', 'Iridium', 'Puffin', 'Tor Browser'
);
$cliBased = array(
'Advanced REST Client', 'Agouti', 'Andoid Debug Bridge (adb)', 'ANTs p2p', 'Apache JMeter', 'ApacheBench', 'aria2', 'aria2c', 'asciinema', 'axel', 'blackfire', 'BlackWidow', 'browsh', 'cadaver', 'clamav-milter', 'Clink', 'curl', 'cURLie', 'davix', 'davtest', 'DirBuster', 'Drupwn', 'DVCS-autosync', 'dwdiff', 'el-get', 'elinks', 'etckeeper', 'FastHttp', 'feedgnuplot', 'fetch', 'Fetch API', 'fetch-mock', 'Fiddler', 'figlet', 'Fio', 'fping', 'ftp', 'GetLeft', 'gh', 'git', 'git-cola', 'git-lfs', 'GNU Wget', 'Go-http-client', 'Googlebot-Image', 'Gopher', 'hping3', 'HTTP_Request2', 'httpclient', 'httperf', 'httpie', 'httpie2', 'httping', 'HttPlaceholder', 'http-prompt', 'HTTPRequest', 'HTTPretty', 'HTTPunit', 'HTTrack', 'httrack2', 'Hurl', 'idn2', 'iMacros', 'imgur-uploader', 'irssi', 'Java', 'jnettop', 'Kali Linux', 'lftp', 'libcurl', 'libwww-perl', 'links', 'lynx', 'mbsync', 'Microsoft BITS', 'Microsoft Office Protocol Discovery', 'Mingw32-wget', 'mod_proxy', 'Mplayer', 'mtr', 'mutt', 'Ncat', 'ncftp', 'Netcat', 'netsh', 'newsbeuter', 'nmap', 'node-fetch', 'Pagekite', 'perl', 'PHP', 'Powershell', 'powershell Invoke-RestMethod', 'Powershell Invoke-WebRequest', 'PowerShell Universal Dashboard', 'PycURL', 'Python Requests', 'Python-urllib', 'rclone', 'RedBot', 'Repeater', 'Requests', 'rsync', 'rtorrent', 'Ruby', 'scp', 'Scrapy', 'screen', 'sftp', 'Siege', 'slimerjs', 'snarf', 'socat', 'SQLMap', 'ssh', 'stunnel', 'Sublist3r', 'tcpdump', 'telnet', 'Tenable Nessus', 'Twisted', 'uGet', 'URLCrazy', 'Urllib', 'urllib3', 'Utopia', 'Veeam Backup', 'w3m', 'WAPT', 'WebDAV', 'Webkit2png', 'Wget', 'Wget+HTTP', 'wget2', 'wget-lua', 'WinSCP', 'WPScan', 'wput', 'Wuzz', 'WWW-Mechanize', 'Yajl', 'Yara', 'YAZ', 'ZAP', 'Zed Attack Proxy', 'Zenmap', 'ZGrab', 'zgrab2', 'Zmap'
);
// Check if the user agent string contains one of the items in the array as a substring, case insensitive
// Checks command line based user agents first, then browsers, since commands like Invoke-WebRequest (PowerShell) will match both
if (preg_match('/' . implode('|', $cliBased) . '/i', $user_agent)) {
// Command Line Based
echo "Command Line Based: " . $user_agent;
} elseif (preg_match('/' . implode('|', $browsers) . '/i', $user_agent)) {
// Browser Based
echo "Browser Based: " . $user_agent;
} else {
// Not matched
echo "Not matched: " . $user_agent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment