Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if visitor is a browser and not a crawler

Tags:

browser

c#

http

How would I server side detect if the user of the webpage is a browser?

The reason for detecting this is, that I'm storing statistics about visited pages and I don't want to store any statistics when the user is a crawler. So I'm not trying to detect which browser vendor. I only want a boolean answer, is the user a browser.

I assume the answer is connected with the user-agent header, but the numerous possibly values is too overwhelming for me to figure out. How can I detect it? A 90% solution where I only detect the most Top5 popular browser would be good enough.

I'm using C# ASP.Net, but I guess most solutions in other languages and frameworks could be translated.

like image 875
Karsten Avatar asked Dec 30 '25 19:12

Karsten


2 Answers

This code is by no means exhaustive, but gives you a basic founding in PHP. I can't guarantee a few won't slip through the net, but this should catch most browsers and ignore most bots.

<?php
// Regular expression to match common browsers
$browserlist = '/(opera|aol|msie|firefox|chrome|konqueror|safari|netscape|navigator|mosaic|lynx|amaya|omniweb|avant|camino|flock|seamonkey|mozilla|gecko)+/i';

// Test for browsers
if (preg_match($browserlist, $_SERVER['HTTP_USER_AGENT'])) {
    // ...is a browser
} else {
    // ...is not a browser
}
?>
like image 90
Rowan Avatar answered Jan 02 '26 08:01

Rowan


If you're using PHP, try $_SERVER['HTTP_USER_AGENT'] or the get_browser() function.

Here's a class that's available for download that makes this process incredibly easy. Example usage:

$browser = new Browser();
if( $browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >= 2 ) {
 echo 'You have FireFox version 2 or greater';
}
like image 24
Donut Avatar answered Jan 02 '26 08:01

Donut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!