0

I have got the following code to redirect IE users to my IE website version:

if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8') )
{
header( 'Location: http://www.web.com/ie/index.php' ) ;
} 

(php tags not included).

But in the other hand I am trying to replace MSIE8 for more versions, and trying the following code does not work.

if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6', 'MSIE 7', 'MSIE 8', 'MSIE 9', 'MSIE 10') )
{
header( 'Location: http://www.web.com/ie/index.php' ) ;
} 

Also trying this one does not work:

if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6, MSIE 7, MSIE 8, MSIE 9, MSIE 10') )
    {
    header( 'Location: http://www.web.com/ie/index.php' ) ;
    } 
Xavi Alsina
  • 643
  • 1
  • 8
  • 24
  • You could store the MSIE 7,8 etc in an array and do your strpos in a foreach() loop. – Oli Apr 15 '14 at 15:47
  • if (left($_SERVER['HTTP_USER_AGENT'], 4)=='MSIE'); – durbnpoisn Apr 15 '14 at 15:52
  • 1
    Reading up on [strpos](http://us2.php.net/strpos), your second attempt may work if you swapped the order of your inputs. i.e. `strpos('MSIE 6, MSIE 7, MSIE 8, MSIE 9, MSIE 10', $_SERVER['HTTP_USER_AGENT'])`. However, read the warning on that link about its return values, it could return `0` which would be evaluated to `false`. – jaredk Apr 15 '14 at 15:57
  • @jaredk Tried your version but seems like it does not work propperly. I'll keep working on it – Xavi Alsina Apr 15 '14 at 16:20
  • My bad, When I say it's not working I mean it is not redirecting, it loads the normal website, not the IE version. When you are saying about checking the value of '$_SERVER['HTTP_USER_AGENT']' what do you exactly mean? Because I am not used to work with PHP – Xavi Alsina Apr 15 '14 at 16:29
  • @XaviAlsina Deleted my earlier comment after doing some more reading into the problem. `$_SERVER['HTTP_USER_AGENT']` is returning a string based on the browser version, see [useragentstring.com](http://www.useragentstring.com/pages/Internet%20Explorer/) for an idea of what strings are being returned. You could try echoing the variable before using it to see what it actually is. Please see [this](http://stackoverflow.com/questions/5302302/php-if-internet-explorer-6-7-8-or-9) question to see if it helps you at all. – jaredk Apr 15 '14 at 16:38
  • Hey @jaredk I saw that post, but I can't find where to add header location (as I said, I'm not used with PHP so my bad...) – Xavi Alsina Apr 15 '14 at 16:55
  • @XaviAlsina Add headers in the same place (as it was working in your first example), but replace your `strpos(...)` part with something like `preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'])` (<-modified from one of the answers in the linked question) – jaredk Apr 15 '14 at 17:38
  • Fine, works perfectly! In the other hand how could I just add the version from IE10 and the previous ones? Because with IE11 I dont need to redirect the user – Xavi Alsina Apr 16 '14 at 08:50
  • @jaredk, do you know any kind of solution for what do I mean? – Xavi Alsina Apr 17 '14 at 10:06
  • @XaviAlsina Please see [this answer](http://stackoverflow.com/a/11741586/1281841) from the question I linked earlier. It mentions that IE11 will use a different kind of UserAgent string, so it should not be picked up by Doug's code in that question. Does that make sense to you? – jaredk Apr 17 '14 at 11:49
  • YEah, thank you for your appreciation and your time! – Xavi Alsina Apr 19 '14 at 18:12

1 Answers1

0

Maybe you can try to use an array (an you will be able to add/remove IE version easily:

<?php
$IEVersion = array();
$IEVersion[] = 'MSIE 6';
$IEVersion[] = 'MSIE 7';
$IEVersion[] = 'MSIE 8';
$IEVersion[] = 'MSIE 9';
$IEVersion[] = 'MSIE 10';

if (in_array($_SERVER['HTTP_USER_AGENT'],$IEVersion)){
     header( 'Location: http://www.web.com/ie/index.php' ) ;
}
?>
Gwenc37
  • 2,064
  • 7
  • 18
  • 22