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:

  1. download the ini file
  2. update (the correct! => fpm) php.ini file to load browscap
  3. 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 for browscap. It should be mentioned there
browscap mentioned in phpinfo()

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:

Click Here to Leave a Comment Below

Leave a Reply: