Browser Detection with PHP
Table of Contents
Did you know that PHP includes a built-in function to detect which browser a visitor is using?
You can try this simple snippet:
<?php
$get_visitor_browser = get_browser(null, true);
print_r($get_visitor_browser);
?>
The get_browser()
function returns an associative array (or object) containing detailed information about the user’s browser, such as:
[browser_name_regex] => ^mozilla/5.0 (windows; .*; windows nt 5.1.*) gecko/.* firefox/1.5.*$
[browser_name_pattern] => Mozilla/5.0 (Windows; *; Windows NT 5.1*) Gecko/* Firefox/1.5*
[parent] => Firefox 1.5
[platform] => WinXP
[browser] => Firefox
[version] => 1.5
[majorver] => 1
[minorver] => 5
[css] => 2
[frames] => 1
[iframes] => 1
[tables] => 1
[cookies] => 1
[javascript] => 1
[javaapplets] => 1
To make this function work correctly, you’ll need an updated browscap.ini
file and must configure PHP to use it.
Add or update the following line in your php.ini
file:
[browscap]
browscap = /path/to/browscap.ini
You can download the latest browscap.ini
file here:
https://browsers.garykeith.com/downloads.asp
More details at the official PHP documentation: https://www.php.net/manual/en/function.get-browser.php