Browscap with PHP
In order to get info about your clients browser, you need to install browscap.
ERROR: get_browser(): browscap ini directive not set
NOTE: you get this same error for different reasons! Check your logfiles to figure out what is the real reason for this error.
Reasons I encountered:
Browscap was not installed
To install Browscap on my Ubuntu (Docker image), I used the following site browscap.org.
It comes down to:
- download the ini file
- update (the correct! => fpm) php.ini file to load browscap
- restart the webserver
In the terminal (or in your Dockerfile):
# install browscap to get browser information of the client
curl https://browscap.org/stream?q=PHP_BrowsCapINI --output ${PHPDIR}/mods-available/php_browscap.ini
# add it to php.ini under the [browscap] directive
sed -i "s|;browscap = extra/browscap.ini.*|browscap = \"${PHPDIR}/mods-available/php_browscap.ini\"|g" ${PHPDIR}/fpm/php.ini
Browscap ini file was not included in php.ini
You can check whether PHP found the browscap file by
- ensuring you entered the correct path to the file in the step above
- ensuring the webserver has read permissions for the file
- place
phpinfo()
on a page and search forbrowscap
. It should be mentioned there
Calling get_browser()
with the wrong arguments
This is a funky one as the ini directive is set.
A look at the laravel log helped: get_browser(): HTTP_USER_AGENT variable is not set, cannot determine user agent name
I used get_browser(null, true)
as I found in an online example, but it really needs the user agent as the first argument.
I fixed it by using: `
get_browser($request->userAgent(), true)
Note that I use Laravel, hence the $request->userAgent()
. You can replace this with $_SERVER['HTTP_USER_AGENT']
just as well.
Other useful links
If this didn’t work out, you might info about your usecase somewhere on these pages: