How To Deal With Bots My Way

If you ever get fed up with bots and spams visiting your site hogging up your server’s CPU usage, send spams emails through the contact form, commenting spam or even unwanted user registration then this is for you.

What if you want to ban specific browser or OS from accessing your site as IP blocking proves ineffective as bots and spammers can change another IP in a matter of time.

I found this neat script that does exactly what I needed, expect I wanted to check against those values rather than print it off. And this works in WordPress’ function as well or even as a new plugin if you wish.

if ($user_os == "Ubuntu"){
  wp_die('<h1>Error</h1><br />Banned');
}

Above example will ban anyone that visit your site through Windows 10, but you can change that according to the list. Is also possible to ban users based on their browser preferences by changing to browser_array.

$browser_array  =   array(
    'msie'       =>  'Internet Explorer',
    'firefox'    =>  'Firefox',
    'yabrowser'  =>  'Yandex Browser',
    'chrome'     =>  'Chrome',
    'safari'     =>  'Safari',
    'opera'      =>  'Opera',
    'netscape'   =>  'Netscape',
    'maxthon'    =>  'Maxthon',
    'konqueror'  =>  'Konqueror',
    'mobile'     =>  'Handheld Browser',
);

$os_array       =   array(
    'windows nt 10.0'    => 'Windows 10',
    'windows nt 6.2'     =>  'Windows 8',
    'windows nt 6.1'     =>  'Windows 7',
    'windows nt 6.0'     =>  'Windows Vista',
    'windows nt 5.2'     =>  'Windows Server 2003/XP x64',
    'windows nt 5.1'     =>  'Windows XP',
    'windows xp'         =>  'Windows XP',
    'windows nt 5.0'     =>  'Windows 2000',
    'windows me'         =>  'Windows ME',
    'win98'              =>  'Windows 98',
    'win95'              =>  'Windows 95',
    'win16'              =>  'Windows 3.11',
    'macintosh|mac os x' =>  'Mac OS X',
    'mac_powerpc'        =>  'Mac OS 9',
    'android'            =>  'Android',
    'ubuntu'             =>  'Ubuntu',
    'linux'              =>  'Linux',
    'phone'             =>  'iPhone',
    'pod'               =>  'iPod',
    'pad'               =>  'iPad',
    'blackberry'         =>  'BlackBerry',
    'webos'              =>  'Mobile'
);

Since Android can be detected as Linux, Yandex Browser not exist or can be detected as Chrome or Chrome detected as Safari in the original source code , I’ve made some changes and ordering to correct them.

You can also add your own browser by visiting this from WhatIsMyBrowser and copy the text within the red box like above and make sure they’re lower case and MUST add the browser before Chrome.

Since Android can be detected as Linux, Yandex Browser not exist in the original source code or can be detected as Chrome or Chrome detected as Safari, so I’ve made some changes and ordering to correct them.

This is very effective if you are looking to ban unwanted visitors to your site. But if blocking a browser or OS ins’t your option, you can always use captcha only if they are tough to crack.

Leave a Reply