Detect browser in preprocess function My goal is to render a warning message to IE 11 users, prompting them to use a modern browser.

I'm a bit of a noob, but from what I understand, it's possible to detect a user's browser using a preprocess function. I found this function online, which looks like it does what I need it to do, but I'm getting a "The website encountered an unexpected error. Please try again later." error when I add it to my THEMENAME.theme file.

function THEMENAME_preprocess(&amp;$variables, $hook){
  if (get_browser_name(htmlentities($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES, 'UTF-8')) == 'Internet Explorer'){
       $variables['browserIs'] = 'IE';
   }
   else{
       $variables['browserIs'] = 'OTHERS';
   }
 }


Any ideas on how I can accomplish what I need?
The function would be get_browser. But you should be using Javascript to detect this instead of PHP.

There are a variety of ways it would fail/be incorrect from the PHP layer, caching, spoofing, etc. Once it gets cached, it would show up for the next person regardless of what browser they are using.

Plenty of JS libs out there can detect it as close as possible, but there is also a module ready to roll here as well that will just solve this for you more than likely, one such module being Browser Detect. There are a few modules for this actually, try them out and see what one suits you best.