0

I want to ensure that my site users are only using chrome and firefox. My hosting company reported unnecessary hit to my site by users coming with empty user agent. This not DOS Attack.

I just want to block all users with empty user agents.

currently the code checks and ensure that only firefox and chrome broswers can access my site.

My question is how do I check users with empty useragent. This is my check for that. Is it right?

//check if useragent is empty
if($agent==''){
echo "user agent is empty. access block";
}

below is the code so far which ensures that only firefox and chrome are allowed access

$arr_browsers = ["Firefox", "Chrome"];
$agent = $_SERVER['HTTP_USER_AGENT'];

$user_browser = '';
foreach ($arr_browsers as $browser) {
    if (strpos($agent, $browser) !== false) {
        $user_browser = $browser;
        break;
    }   
}


//check if the browser is not in array

if ( ! ( in_array($user_browser, $arr_browsers) ) ) {

echo "you browser are not allowed. This work fine";

}
jmarkatti
  • 621
  • 8
  • 29
  • 1
    Possible duplicate of [How to get user agent in PHP](https://stackoverflow.com/questions/10243841/how-to-get-user-agent-in-php) – hostingutilities.com Jul 12 '19 at 17:30
  • If you're being DOSed you're still going to take a hefty resource hit by letting these requests hit PHP at all. You should kill the request at the Apache/Nginx level, if not before. – Sammitch Jul 12 '19 at 23:44

1 Answers1

0

Your test for an empty user agent looks fine to me.

In the second code, there's no need to check if $user_agent is in the array. If it's not the empty string that you initialize it with, it must be one of the values from the array. So just write

if ($user_browser == '') {
    echo "your browser is not allowed";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • okay thanks thats is great to hear. Sir why I also check arrays is to ensure that only users from chrome and firefox can access the site. I can add other browsers later. I hope the array check is also okay. Finally Sir Barmar.I have one more question. will the above check especially the array check stops googlebots, yahoobots and other good bots from indexing my site. will it affects my page ranking. Hope to hear from you thanks – jmarkatti Jul 12 '19 at 16:26
  • I understand that. You're already checking that with the loop. – Barmar Jul 12 '19 at 16:31
  • If you do this in all your pages, you will almost certainly affect your page ranking. You should add the bots you want to your array of allowed browsers. – Barmar Jul 12 '19 at 16:32
  • Thanks I will check out for good bot user agents like googlebot, yahoobot etc and then add them. Thanks alot Sir Barmar – jmarkatti Jul 12 '19 at 16:35
  • @jmarkatti also consider defining a [robots.txt](https://moz.com/learn/seo/robotstxt) so that well-behaved bots know what your rules are so they can follow them. Of specific interest is `crawl-delay`. Bots that _don't_ follow the rules get added to the sh*t list and have their user-agents blocked. – Sammitch Jul 12 '19 at 23:47