How to detect Internet Explorer and firefox using PHP?

How to detect Internet Explorer and firefox using PHP?

0
source share
6 answers

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

However, keep in mind that this can be changed by the user, and some browsers even provide the ability to do it VERY simply (for example, Konqueror). Many plugins are available for this.
Never trust this line.

+3
source

The easiest way is to use the PHP get_browser function, as this will parse the HTTP HTTP user header and extract the appropriate browser, versions, platforms, etc. to an array or object as needed.

Running this (in array mode for the purposes of this example) will return the data structure in the following format (using the current php_browscap.ini file from Browser Capability Project dated January 15, 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 ) 

NB: According to the PHP manual page:

"For this to work, the cast configuration in php.ini must point to the correct location of the browsercap.ini file on your system."

+2
source

So not what you requested. But it is often more sensible to distinguish between functions:

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

None of the trash versions of IE support XHTML. So a good way to split browsers. Note that IE9, however, takes into account the new class and can be treated as comparable to Firefox.

+1
source

If you need something just to put some HTML code (for example, in another stylesheet), use something similar (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 many IE add-ons (and some spyware) change parts of useragent.

0
source

To discover Firefox

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

To discover IE

 $isIe = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') > -1); 
0
source
  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; 
-one
source

Source: https://habr.com/ru/post/1395583/


All Articles