1

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(&$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?

4
  • 4
    The function is get_browser. php.net/manual/en/function.get-browser.php but you should be using Javascript to detect this instead of PHP.
    – Kevin
    Oct 30 '19 at 17:10
  • @Kevin My thought was that this would work even if JS is disabled, but it sounds like JS is the way to go?
    – Kellen
    Oct 30 '19 at 17:12
  • 4
    There are a variety of ways it would fail/be incorrect from the PHP layer, caching, etc. Plenty of JS libs out there that do just that, but there is also a module ready to roll here as well that will just solve this for you more than likely: drupal.org/project/browser_detect THere are a few modules for this actually, try them out and see what one suits you best.
    – Kevin
    Oct 30 '19 at 17:20
  • 2
    @Kevin You should write that up as an answer.
    – mpdonadio
    Oct 31 '19 at 2:15
1

Use the Browser detect module , it will add browser class to html tag as well.

This project allows you to identify the browser and the device used.

A class is inserted in the HTML tag so you can use as you need.

Also, you can detect it by calling this function after install this module

 Drupal.behaviors.browser_detect.browserVersion();
1
  • 1
    Unfortunately this doesn't explain why trying to add that class from a preprocess function is no good idea (caching).
    – leymannx
    Oct 31 '19 at 20:10
2

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.

2
  • 1
    Originally there's Browser Class. I initiated the 8.x branch a while back but didn't set it to be RC or stable, but actually it contains more or less the same code as Browser Detect (which was created only 2 months ago). Browser Class usage is much higher than Mobile Detect usage. I'd wish Mobile Detect would put their efforts in the already existing module instead.
    – leymannx
    Nov 1 '19 at 21:01
  • 2
    Yes I agree I wish there was a d.o committee around enforcing a single solution instead of have multiple modules that do basically the same thing. It is confusing for end users.
    – Kevin
    Nov 1 '19 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.