How to detect a Users browser?
Browser detection in PHP is pretty easy.
Note: If anything from below doesn’t work for you, then see the troubleshooting tips at the end of this page.
For detecting the browser, we’re using PHP’s built-in function ‘get_browser()‘
This function accepts 2 arguments: The User Agent and a boolean value for whether to return the result in an array or not.
$browser_info = get_browser(null, true); //Store Browser info in an associative array
$browser_name = $browser_info['browser']; //Extract Browser name from the associative array
echo $browser_name; //Print the browser name
- Open your ‘php.ini‘ file from your PHP installation.
For XAMPP users, it is located in the folder: ‘C:\xampp\php‘. Open the file in Notepad.
- Find ‘browscap‘ in it. (Tip: Do CTRL+F for quick find.)
You will find something like browscap = “<browscap.ini_path_here>”
For XAMPP users, it should be: browscap = “C:\xampp\php\extras\browscap.ini”
Although it might be placed in a different folder in newer versions.
- Verify that your browscap.ini exists in the path mentioned above.
If it is not there then find browscap.ini in your computer or download it from here or search for the latest 1 here.
- Either place the browscap.ini file in the folder specified in your php.ini file.
Or, place the browscap.ini file in your desired folder and point your php.ini file to it.
- Restart your Apache/XAMPP/Wamp or whichever client you are using.
- Try the above program again.