Activate <div> based on User Agent
-
22-07-2019 - |
Question
I need to activate a "div" based on the user agent string. For example, (and this is my actual use for this,) A user with Firefox comes to my site. I want to tell them something along the lines of "Welcome Firefox User! Thank you for supporting Open Source Software, etc;".
That message is inside a Div. I would like to just show it for firefox users. It has to be compatible with most modern (mainstream) browsers. I am using PHP.
Thank you in advance.
Artur
Solution
On server-side with PHP, you can inspect the user's browser string with the built-in $_SERVER['HTTP_USER_AGENT'] variable. Then, based on its value, you can output a DIV.
OTHER TIPS
Try this:
<?php if(strpos($_SERVER["HTTP_USER_AGENT"], "Firefox") !== FALSE) { ?>
<div>Thanks for supporting Firefox!</div>
<?php } ?>
Not very reliable, though. I answered just so I could do the conditional-drop-out-of-PHP thing, which I abhor.
You can use server side detection, but as you've not specified what language you are using, I can only suggest a frontend (javascript) method.
jQuery has a utility function jQuery.browser()
which will return one of a number of "flags" based on the useragent string. Take a look at the docs at the link below for more information.