Browser Specific Redirect

Discussion in 'Programming' started by alecrust, Sep 1, 2007.

  1. #1
    Hello,

    I am looking for the best way to redirect the user to a different site, if they are using a version of Internet Explorer under 7.

    What would be the best way to do this?

    Thanks
     
    alecrust, Sep 1, 2007 IP
  2. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    $vernum=array();
    
    $regex = '/MSIE ([0-9]+)\..+;/';
    
    preg_match($regex,$_SERVER['HTTP_USER_AGENT'],$ver);
    
    if (count($ver) < 2)
    {
    	// not IE at all
    }
    else
    {	
    	// IE
    	if ($ver[1] < 7)
    	{
    		// version < 7 so redirect
    		header('Location: http://www.example.com/');
    		exit;	
    	}
    	else
    	{
    		// version >= 7
    	}
    }
    
    PHP:
     
    sea otter, Sep 1, 2007 IP
  3. alecrust

    alecrust Member

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Many thanks for that sea otter!

    I was wondering:

    Would I simply ut that at the top of each page I wanted redirecting?

    Also, is it better to use your code or this:
    
    <!--[if lt IE 7]>
    <script type="text/javascript">
    window.location = "http://www.alecrust.com/ie/";
    </script>
    <![endif]-->
    
    Code (markup):
     
    alecrust, Sep 1, 2007 IP
  4. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can put it in a php file, and then include that php file at the top of each page. That way, if you ever change the redirection url or criteria, you only have to edit one file.

    Put the code from above in a file called redir.php and then include it in every file you want checked:

    
    <?
       include_once 'redir.php';
    
    
      // if you get here, you haven't redirected.
    ?>
    
    PHP:
    As for the javascript, well yes, you can do that but it won't work for browsers with javascript disabled, and the code only gets called once your original page has downloaded and all the php in it has executed.

    The php code, on the other hand, executes before anything is sent back to the browser, so the redirect will happen without a flicker and without any other server-side code executing.

    Client-side detection is usually not done for redirects, but for customizing the behavior of various javascript DOM/event functions and CSS directives to accommodate specific browser quirks.
     
    sea otter, Sep 1, 2007 IP
  5. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I assume that you are wanting to make sure for every page that the user is looking at the correct version so for pre IE7 they look at http://www.yourdomain.com/ie/pagename.aspx where as everyone else sees http://www.yourdomain.com/pagename.aspx ?

    First of all would question why you need to maintain 2 versions of your site. If there are problems with the CSS or such there are better ways of handling this.

    The solution we would use is to add the following to the onload in the master file.
    
    ' get the page address
        Dim pStr As String = Request.RawUrl
    
    ' check which browser & react
        If Request.Browser.Browser = "IE" And Request.Browser.MajorVersion < 7 Then
    
            If Not pStr.StartsWith("/ie/") Then Response.Redirect("/ie" & pStr)
    
        Else
    
            If pStr.StartsWith("/ie/") Then Response.Redirect(pStr.Remove(0, 3))
    
        End If
    
    Code (markup):
     
    AstarothSolutions, Sep 2, 2007 IP