How to Detect IE6 with PHP

How to perform Browser detection - IE (versions) in PHP

Try this:

<?php
$ie6 = (ereg("MSIE 6", $_SERVER["HTTP_USER_AGENT"])) ? true : false;
$ie7 = (ereg("MSIE 7", $_SERVER["HTTP_USER_AGENT"])) ? true : false;
$ie8 = (ereg("MSIE 8", $_SERVER["HTTP_USER_AGENT"])) ? true : false;

if ($ie6 || $ie7 || $ie8) {
// Do fallback stuff that old browsers can do here
echo "You are using IE";
} else {
// Do stuff that real browsers can handle here
}
?>

PHP: If internet explorer 6, 7, 8 , or 9

This is what I ended up using a variation of, which checks for IE8 and below:

if (preg_match('/MSIE\s(?P<v>\d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B) && $B['v'] <= 8) {
// Browsers IE 8 and below
} else {
// All other browsers
}

Can I detect IE6 with PHP?

Try checking their user agent for 'MSIE 6.'.

$using_ie6 = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE);

This is based on this user agent information.

What's the best way to detect a browser with php?

You can't reliably detect what the browser is. Its as simple as that.

Browsers are capable of lying about their identity, and frequently do.

Some proxies and security products strip the user agent data from the request, so your PHP code may just get an empty string.

In Javascript you may have a bit more luck, though it's still problematic, but at the PHP level you can never really be certain.

My first suggestion would be to drop support for IE6, unless your demographic is in the really stubbon minority. IE6 usage stats is down to below 2.5% in most developed countries now. That alone will get rid of a large part of your CSS problems. IE7 is still not good, but it's a clear mile better than IE6, and it is just about supportable while sticking to modern standards.

My second suggestion is rather than trying to downgrade your site for these browsers, try to upgrade the browser.

There are a number of very good hacks and tools that will allow you to improve CSS support in older versions of IE. I recommend you try the following:

  • CSS3Pie
  • Selectivizr
  • Modernizr
  • Dean Edwards' IE7.js

And of course, the ubiquitous JQuery.

PHP detect Internet Explorer that is below version 10

The function get_browser() may be used for feature detection.

If you really want to only know the user's browser version you can look at the $_SERVER['HTTP_USER_AGENT'].

A word of caution: although most users won't do this, it is possible to send a different user agent string to the server which might be done to e.g. protect ones privacy. If someone wants to go as far as sending you a user agent to be able to use your website though they'll probably know how to deal with any bugs that may come up.

As a side question, what is the reason you're trying to limit your audience to IE 10 and up? Why not use a library such as Modernizr to fill in functions you might be missing in IE 9?

EDIT:
Some info regarding the IE 10 user agent string. You will want to read that so you know what to match your $_SERVER['HTTP_USER_AGENT'] against

How to Block a browser after detecting it with php on my website?

I solved it using this

    <?php

class Browser
{
public static function detect()
{
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
if ((substr($_SERVER['HTTP_USER_AGENT'], 0, 6) == "Opera/") || (strpos($userAgent, 'opera')) != false) {
$name = 'opera';
} elseif ((strpos($userAgent, 'chrome')) != false) {
$name = 'chrome';
} elseif ((strpos($userAgent, 'safari')) != false && (strpos($userAgent, 'chrome')) == false && (strpos($userAgent, 'chrome')) == false) {
$name = 'safari';
} elseif (preg_match('/msie/', $userAgent)) {
$name = 'msie';
} elseif ((strpos($userAgent, 'firefox')) != false) {
$name = 'firefox';
} else {
$name = 'unrecognized';
}
if (preg_match('/.+(?:me|ox|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name'] == 'safari') {
$version = $matches[1];
}
if (preg_match('/.+(?:me|ox|it|on|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name'] != 'safari') {
$version = $matches[1];
} else {
$version = 'unknown';
}

return array(
'name' => $name,
'version' => $version,
);
}
}

$browser = Browser::detect();
echo 'You browser is ' . $browser['name'] . ' version ' . $browser['version'];
echo "<br />";
?>

I found the solution in this thread.

Furthermore, you can add a condition and use die().

How can I detect the browser with PHP or JavaScript?

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;
}
}
}


Related Topics



Leave a reply



Submit