-1

how to detect internet explorer and firefox using PHP?

Bronislaw
  • 85
  • 2
  • 10
  • Why would you want to do this on the server side? – John Parker Jan 15 '11 at 23:04
  • to customize the page according to browser – generalhenry Jan 15 '11 at 23:05
  • 1
    @generalhenry If that's the case, I'd have thought conditional stylesheets would be the way to go, hence why I'm asking. (Then again, perhaps the OP wants different context or summat.) – John Parker Jan 15 '11 at 23:07
  • @generalhenry I share @middaparka s point of view. User Agent detection may make only sense if you're providing alternate content for different devices (such as smartphones). Use stylesheets to style and always provide a method to switch to a different content. – Samuel Herzog Jan 15 '11 at 23:10
  • it's also useful for usage statistics, but you're right user agents are easy to spoof so conditional sheets are more effective – generalhenry Jan 15 '11 at 23:11
  • @generalhenry of course it is, thats the reason why I answered :) Just be sure to use this variable (and also `get_browser()`) carefully. – Samuel Herzog Jan 17 '11 at 02:17

6 Answers6

3

you can use $_SERVER['HTTP_USER_AGENT'] as found in the php manual.

However, be aware that this can be changed by the user and some browsers even provide the ability to do this VERY easy (e.g. Konqueror). A lot of plugins are available to do the same.
Never ever trust this string.

Samuel Herzog
  • 3,561
  • 1
  • 22
  • 21
2

The easiest way is to use PHP's get_browser function, as this will parse the HTTP User-Agent header for you and extract the relevant browser, version, platform, etc. information into an array or object as required.

Running this (in array mode for the purposes of this example) will return a data structure in the following format (using the current php_browscap.ini file from the Browser Capabilities Project as of 15th Jan 2011):

Array
(
    [browser_name_regex] => �^mozilla/5\.0 \(windows; u; windows nt 6\.1; .*\) applewebkit/.* \(khtml, like gecko\) chrome/8\..* safari/.*$�
    [browser_name_pattern] => Mozilla/5.0 (Windows; U; Windows NT 6.1; *) AppleWebKit/* (KHTML, like Gecko) Chrome/8.* Safari/*
    [parent] => Chrome 8.0
    [browser] => Chrome
    [platform] => Win7
    [version] => 8.0
    [majorver] => 8
    [win32] => 1
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [javaapplets] => 1
    [javascript] => 1
    [cssversion] => 3
    [supportscss] => 1
    [minorver] => 0
    [alpha] => 
    [beta] => 
    [win16] => 
    [win64] => 
    [backgroundsounds] => 
    [cdf] => 
    [vbscript] => 
    [activexcontrols] => 
    [isbanned] => 
    [ismobiledevice] => 
    [issyndicationreader] => 
    [crawler] => 
    [aol] => 
    [aolversion] => 0
)

N.B.: As per the PHP manual page:

"In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system."

John Parker
  • 54,048
  • 11
  • 129
  • 129
0

To detect Firefox

$isFirefox = (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') > -1);

To detect IE

$isIe = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') > -1);
ElephantHunter
  • 1,552
  • 1
  • 11
  • 15
0

So that's not what you asked for. But it's often more senseful to differentiate on features:

 if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) {
      // Firefox, Safari, Opera, Chrome, IE9
 }
 else {
      // IE5,IE6,IE7,IE8
 }

None of the garbage versions of IE supports XHTML for example. So that's a good way to separate browsers. Note how IE9 however counts into the newer class, and might actually be treated comparable to Firefox.

mario
  • 144,265
  • 20
  • 237
  • 291
0

If you need something just to put some HTML code (like different stylesheet attached) use something like that (instead of server-side code).

<!--[if IE]>
        <link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->

Otherwise use the HTTP_ACCEPT solution because it is based on browser features not just name. Especially that a lot of IE-addons (and some spyware) change some parts of useragent.

Chris Hasiński
  • 2,965
  • 2
  • 25
  • 34
-1
 if (isset($_SERVER['HTTP_USER_AGENT'])) {
   $useragent   = $_SERVER['HTTP_USER_AGENT'];
 }

 if (strlen(strstr($useragent  , 'MSIE')) > 0) {
    $browser = 'internet explorer';
 }else if (strlen(strstr($useragent  , 'Firefox')) > 0) {
    $browser = 'firefox';
 }else{
   $browser = 'others';
 }

 echo $browser;
  • According to my knowledge Its already working for me, why couldn't you use it ? I should know, then i can improve this answer. cause i am not seeing any fault here. – Md. Salman Fahad Famous Oct 29 '17 at 11:02