6

Possible Duplicate:
Any php code to detect the browser with version and operating system?
how to detect internet explorer and firefox using PHP?

I need to add specific html to non firefox browsers, therefore I need to first detect if user is using firefox or not, i have to do this using php. Something along these line?

<!--[if Firefox]>
Mozilla only HTML here!
<![endif]-->

Any one? Thanks

Community
  • 1
  • 1
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • 1
    By the way, the example you gave is conditional HTML comment, not PHP. – JJJ Feb 09 '12 at 11:04
  • Sorry, completely missed those posts. Yes, I am aware that html, it was just a quick example, can't i do it using plain html conditionals btw? – rob.m Feb 09 '12 at 11:14

6 Answers6

18

Use this snippet of code to retrieve the kind of browser:

if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $agent = $_SERVER['HTTP_USER_AGENT'];
}

Then, compare against the user agent string.

For example, to detect "Firefox" you could do:

if (strlen(strstr($agent, 'Firefox')) > 0) {
    $browser = 'firefox';
}
ElephantHunter
  • 1,552
  • 1
  • 11
  • 15
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • what should i write as "HTTP_USER_AGENT" ? firefox, FIREFOX, Firefox, mozilla, MOZILLA, Mozilla, gecko, GECKO, Gecko? – rob.m Feb 09 '12 at 11:07
  • so: if(strlen(strstr($agent,"Firefox")) > 0 ){ $browser = 'firefox'; do something... } else { do something else }; ? – rob.m Feb 09 '12 at 11:12
  • 1
    Yes. If you detect that is firefox the user agent, let's insert the snippet of code you have to insert into html (by appending it? could be a solution) – DonCallisto Feb 09 '12 at 11:14
  • You can just use strpos, which is way much faster than your unnecessary strlen and strstr. – user3304007 Sep 23 '16 at 22:12
2

php's inbuilt function get_browser is your answer. or try using $_SERVER['HTTP_USER_AGENT'] or simply use this script http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

Jaspreet Chahal
  • 2,759
  • 1
  • 15
  • 17
1

Use the get_browser function:

http://php.net/manual/en/function.get-browser.php

davidjwest
  • 538
  • 3
  • 22
1

You can use the User-Agent header in the request. Either you do a regex check on it to find Firefox specific patterns or you can use a library like WURFL that has a better chance of identifying the browser.

See this answer for an example on how to do it.

Community
  • 1
  • 1
Alex Ciminian
  • 11,398
  • 15
  • 60
  • 94
1

Use $_SERVER['HTTP_USER_AGENT'] to get user browser.

giker
  • 4,165
  • 1
  • 18
  • 10
0

look in $_SERVER['HTTP_USER_AGENT']

e.g.: Mozilla/5.0 (Windows NT 5.1; rv:13.0a1) Gecko/20120206 Firefox/13.0a1

k102
  • 7,861
  • 7
  • 49
  • 69