tags:

views:

322

answers:

7

how can i detect if the user is not using any of (chrome, firefox or ie) browser with js or php

+5  A: 

There is actually a function in php for that. get_browser http://php.net/manual/en/function.get-browser.php

txyoji
Very interesting, I was unaware of this function. thx for the info
That function requires browsecap.ini, which is not core PHP.
Oz
True. Here is a pure php version of this function. It auto-updates the browsecap file too. http://code.google.com/p/phpbrowscap/
txyoji
+1  A: 

This perhaps.

GrayWizardx
A: 

Here, I use this for php

Digital Craft Studios
+5  A: 

here is a javascript code through which you can easily detect browser.

    var userAgent = navigator.userAgent.toLowerCase();

    // Figure out what browser is being used
    var Browser = {
        Version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
        Chrome: /chrome/.test(userAgent),
        Safari: /webkit/.test(userAgent),
        Opera: /opera/.test(userAgent),
        IE: /msie/.test(userAgent) && !/opera/.test(userAgent),
        Mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
        Check: function() { alert(userAgent); }
    };


    if (Browser.Chrome || Browser.Mozilla) {
        // do your stuff for firefox and chrome
    }
    else if (Browser.IE) {
        // do something related to IE
    }
    else {
        // The browser is safari, opera or some other
    }
Zain Shaikh
A: 

In PHP I use the $_SERVER['HTTP_USER_AGENT'] value and attack it with regex or stristr.

Oz
A: 

The simplest way to do it with Javascript is

<script language="Javascript">

location.href = 'URL_TO_FORWARD_TO';

</script>

within the location.href, you could use PHP variable like so:

<script language="Javascript">

location.href = '<?php echo $_SERVER['QUERY_STRING']; ?>';

</script>

This would take a URL given as a query to the PHP script and forward to that URL. The script would be called like this:

http://your-server/path-to-script/script.php?URL_TO_FORWARD_TO

Good Luck

Carlson Technology
Answer to the wrong question perhaps?
Blair McMillan
A: 

The best way to do this in JS I found is on Quirksmode. I made one for PHP which should work with common browsers :

  $browser = array(
    'version'   => '0.0.0',
    'majorver'  => 0,
    'minorver'  => 0,
    'build'     => 0,
    'name'      => 'unknown',
    'useragent' => ''
  );

  $browsers = array(
    'firefox', 'msie', 'opera', 'chrome', 'safari', 'mozilla', 'seamonkey', 'konqueror', 'netscape',
    'gecko', 'navigator', 'mosaic', 'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol'
  );

  if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $browser['useragent'] = $_SERVER['HTTP_USER_AGENT'];
    $user_agent = strtolower($browser['useragent']);
    foreach($browsers as $_browser) {
      if (preg_match("/($_browser)[\/ ]?([0-9.]*)/", $user_agent, $match)) {
        $browser['name'] = $match[1];
        $browser['version'] = $match[2];
        @list($browser['majorver'], $browser['minorver'], $browser['build']) = explode('.', $browser['version']);
        break;
      }
    }
  }
Fabien Ménager