views:

107

answers:

3

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

+1  A: 

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.

jQuery.browser() @ jQuery Docs

Chris
+3  A: 

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.

Alex
How would I output a div? Like, code wise? (Javascript is fine. I have jQuery installed.
playfulcyanide
echo "<div>Hello world</div>";
Alex
Yes, but how would I define it's visibility based on the User Agent?
playfulcyanide
if(strpos($_SERVER["HTTP_USER_AGENT"], "Firefox") !== FALSE) { echo "<div>hello world</div>"; }
Alex
+1  A: 

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.

Jed Smith
Is there a way to specify the OS it's running on? Like, only for Mac Firefox?
playfulcyanide
Rich Bradshaw