views:

878

answers:

5

My web site want to be open IE7 and above .If IE 6 ,I want to produce warning and free download other browser icons .Is it possible?

A: 

You can check the $_SERVER['HTTP_USER_AGENT'] variable for IE.

if (eregi("MSIE", $_SERVER["HTTP_USER_AGENT"]) ||
   eregi("Internet Explorer", $_SERVER["HTTP_USER_AGENT"])) {
   // IE
}
Josef
Specifically, you need to look for the string "MSIE 6.0" in this variable.
Yuval F
He said IE6, not IE entirely.
Esko
+8  A: 

You can get some examples that don't require server side scripting from ie6nomore.com.

They use the conditional comments feature of IE, like this:

<!--[if lt IE 7]>
Your browser is outdated!
<![endif]-->

But the examples on the site actually offer links to other browsers. Of course, you can roll your own version that suits your layout better.

Of course, you can do this server side if you prefer, since you're using PHP anyway. The other examples here using $_SERVER["HTTP_USER_AGENT"] should get you started. Using get_browser may be overkill, as it requires a fairly large data file to function.

If you're only interested in detecting old IE versions server side, this should do:

preg_match('/; MSIE (\d+.\d+)/', $_SERVER['HTTP_USER_AGENT'], $matches);

if (count($matches) > 1 && $matches[1] <= 6.0)
{
    echo "Your browser is outdated";
}
Thorarin
+1  A: 

Use IE conditional comments in your page

<!--[if lt IE 7]>
include a warning here (in an iframe, perhaps, to save extra bandwidth)
<![endif]-->
Steve Gilham
A: 

You should be able to do so easily by using the built-in get_browser function.

In case you wish to see what the output looks like if visited by IE 6, you can grab a user agent string from UserAgentString.com to test it out.

Sebastian P.
A: 

Alternatively you could also check it with a JavaScript

/*
*   Check whether the current browser is IE6
*/
function isBrowserIE6() {
    if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7) {
        return true;
    } else {
        return false;
    }
}

(The above would just work in a ASP.net environment. Here's a blog post which handles IE browser checking the native way).

You then add an HTML container element on your page

<div id="ie6BrowserWarning" style="display:none">
   Your browser is outdated. Please download one of the alternative browsers!
   <!-- Set of links to Firefox, Chrome, Safari, Opera,... -->
</div>

And on page load you do

<html>
<head>
<script type="text/javascript">

  function doIE6WarningCheck()
  {
     var element = document.getElementById("ie6BrowserWarning");
     var isIE6 = isBrowserIE6();
     if(element != null && isIE6 == true)
     {
        element.style.display = "block";
     }
  }
</script>
</head>
<body onLoad="doIE6WarningCheck()">
   <div id="ie6BrowserWarning" style="display:none">
      Your browser is outdated. Please download one of the alternative browsers!
      <!-- Set of links to Firefox, Chrome, Safari, Opera,... -->
   </div>
</body>
</html>

I didn't check that, just wrote it out of my head right now. You'd have to do that, but I guess it should work. Firebug is always a good option for JavaScript debugging.

Juri
That isn't standard JavaScript. It only works in an ASP.NET environment. Not very helpful since he's using PHP. If you want to do this client side, conditional comments are much easier.
Thorarin
Sorry, I took the isBrowserIE6() from one I've written a couple of time ago. Didn't pay attention that it will just run on ASP.net. Of course conditional comments are easier, JavaScript would just have been an alternative. (I updated the post to point to a page which contains native browser checking).
Juri