I am building a site that when viewed via IE 6 or under, there is a redicrection to a page offering a link to upgrade to version 7+.
file: browserDetect.inc.php
<?php
error_reporting(E_ALL);
if (preg_match('#MSIE (\d+)#', $_SERVER['HTTP_USER_AGENT'], $match)){
if ($match[1] < 7){
header('Location: /cocoa/invalidBrowser.php');
exit;
}
}
?>
top few lines in index.php
<?php
session_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/somefolder/browserDetect.inc.php'); // check for valid browser use
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
header('Content-type: text/html; charset=iso-8859-1');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...head stuff goes here...
</head>
What is happening here is that via a require_once, I am requiring a file (browserDetect.inc.php) that checks to see if IE 6 or less is detected, and if so, a redirect takes place. Note the location of the require_once line in the index.php file... it is just above the line that checks to see if gzip is involved. Now this all executes perfectly, but image my surprise to see that this doesn't validate according to the w3c validator.. it complains of things like:
the MIME Media Type (text/html) can be used for XML or SGML document types
No known Document Type could be detected
No XML declaration (e.g <?xml version="1.0"?>) could be found at the beginning of the document.
No XML namespace (e.g <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">) could be found at the root of the document.
so to solve this, I simply change the location of require_once to be below the gzip line.
Correct index.php
<?php
session_start();
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/somefolder/browserDetect.inc.php'); // check for valid browser use
header('Content-type: text/html; charset=iso-8859-1');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...head stuff goes here...
</head>
Presto! It valides perfectly.
So my question out of all this is.. how is it that when require_once is above the gzip line, the code has errors, while it validates if placed below instead. If you look at the browserDetect.inc.php file, if no MSIE is detected, nothing happens... I'm rather curious (and confused) as to invalidating one way and validating the other.
Cheers