Reputation: 561
So here's my quandary. I have a website that's very sensative to user agents. It is absolutely imparative that the correct user agent is sent to the site, or the site will not work for that user. Site works fine, unless a user is directed there via permanant redirect.
That's where it gets interesting. See, Firefox and Chrome have no issues. But IE starts lying about what version of the browser you're using the minute it sees the status and the domain change. I have a client (a very big slow client) linking my site from the outside via permanant redirect. And I don't think this client has the ability to change that right now. So I'm stuck with it, and my browser detection goes wonky when IE goes from being:
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
to:
Mozilla/4.0 compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3
Some things I've tried: Added the meta tag that changes the rendering engine:
<meta content="IE=7" http-equiv="X-UA-Compatible" />
Works in IE 8, but 9 ignores it. Tried detecting different versions of .net framework, but it's not always reliable. Tried using a canvas tag in Javascript, but that only broke it in IE 7 and sometimes 8.
I'm thinking there has to be some kind of header I can push to it, but I don't know of any.
Has anyone seen this before? Is there a functional workaround?
Edit: Someone suggested modernizer. That's an awesome idea.
The other thing I did to resolve this was rewrite my browser detection script.
Here it is, in case anyone ever finds themselves in with a similar problem: It's dirty, but it works.
function getBrowser() {
//shamlessly "borrowed" from the manual at http://php.net/manual/en/function.get-browser.php
//Needed some cleanup.
//Still needs some cleanup
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version = "";
$xploded = explode(';',$u_agent);
//pretty($xploded);
// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) {
$version = $matches['version'][0];
} else {
$version = $matches['version'][1];
}
} else {
$version = $matches['version'][0];
}
// check if wfunction getBrowser() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version = "";
//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
} elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
} elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}
// Next get the name of the useragent yes seperately and for good reason
if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) {
$bname = 'Internet Explorer';
$ub = "MSIE";
} elseif (preg_match('/Firefox/i', $u_agent)) {
$bname = 'Mozilla Firefox';
$ub = "Firefox";
} elseif (preg_match('/Chrome/i', $u_agent)) {
$bname = 'Google Chrome';
$ub = "Chrome";
} elseif (preg_match('/Safari/i', $u_agent)) {
$bname = 'Apple Safari';
$ub = "Safari";
} elseif (preg_match('/Opera/i', $u_agent)) {
$bname = 'Opera';
$ub = "Opera";
} elseif (preg_match('/Netscape/i', $u_agent)) {
$bname = 'Netscape';
$ub = "Netscape";
}
// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) {
$version = $matches['version'][0];
} else {
$version = $matches['version'][1];
}
} else {
$version = $matches['version'][0];
}
// check if we have a number
if ($version == null || $version == "") {
$version = "?";
}
$bname = strtolower($bname);
$windows_version = floatval(trim(str_ireplace('Windows NT','',$xploded[2]) ));
//Sloppy hack for dealing with IE 9, specifically.
$trial = intval($version);
if($windows_version == 6.1 && $bname == 'internet explorer' && $trial == 7){
//the browser is lying to you.
//print "We are being lied to<br>";
$version = 9.0;
}
//print 'windows version is......'.$windows_version.'<br>';
//print 'browser id is......'.$bname.'<br>';
//print 'raw version is......'.$version.'<br>';
return array(
'test'=>'test',
'name' => $bname,
'version' => intval($version)
);
}
Upvotes: 1
Views: 661
Reputation: 7204
Unfortunately, since you've inherited a site that originally did the wrong thing, you are going to have to slowly start porting pieces of functionality toward using feature detection rather than UA sniffing.
I would advise you to get Modernizer installed and slowly start porting. Start with the most broken page(s) and go from there.
Upvotes: 3