Lothar Lothar - 4 months ago 11
PHP Question

Code Igniter - Best way to detect browser

I want to shunt all browsers of a certain age off to their own page. What is the best method for doing this? Perhaps some JS in the header that is wrapped in :

<!--[if lte IE 7 ]>
<script type="text/javascript">
window.location = "/unsupported-browser/";
</script>
<![endif]-->


Shouldn't the above send the browser to: http://example.com/unsupported-browser/ where I have a basic controller and view to handle it? Is it that simple?

Answer

Do this in php instead. Use the user_agent class and redirect to that page.

But more importantly, why don't you allow IE users access to your site? Is it due to CSS or something else?

Code:

$this->load->helper('url');
$this->load->library('user_agent');

if ($this->agent->browser() == 'Internet Explorer' and $this->agent->version() <= 7)
    redirect('/unsupported-browser');

Edit:

As mentioned; if you want this over your entire site, run this in MY_Controller and make sure to add $this->uri->segment(1) != 'unsupported-browser' as an extra condition to avoid redirect loops.