2

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?

will_d
  • 1,126
  • 7
  • 7
Lothar
  • 3,409
  • 8
  • 43
  • 58

2 Answers2

16

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.

Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
  • OP said shift to a different page not deny access, but user_agent is what id do – allen213 Apr 20 '12 at 08:03
  • 1
    Based on example it looks like they will be sent to a page saying "This site is not available in your browser, please upgrade it" – Robin Castlin Apr 20 '12 at 08:05
  • yeah you are right, not a good thing to do but often im tempted – allen213 Apr 20 '12 at 08:09
  • Yeah, I hear you man. I'd prefer if everyone used latest mozilla and webkit browser so I always knew what my users gained as a result. It all depends on how you many on the internet you wish your service to be functional for. Think if Facebook had a /unsupported-browser page, for instance :) – Robin Castlin Apr 20 '12 at 08:12
  • For some reason this redirect always goes to: mysite.com/?/unsupported-browser Any idea why the ? is getting added to the path? – Lothar Apr 20 '12 at 18:13
  • Troubleshoot it. Echo `$this->agent->browser()` and see if it's 'Internet Explorer'. – Robin Castlin Apr 20 '12 at 19:50
  • If You use this code in MY_Controller.php you will have redirect loop. Try to add `$this->uri->segment(1) != 'unsupported-browser'` into IF statement. – Tomasz Majerski Feb 19 '15 at 09:52
  • Yepp, that's true. Updated answer. – Robin Castlin Feb 23 '15 at 15:33
2

Download library from http://mobiledetect.net

Put Mobile_Detect.php in to 'libraries'

inside main controller

public function index() {
    $this -> load -> library('Mobile_Detect');
    $detect = new Mobile_Detect();
    if ($detect->is('Chrome') || $detect->is('iOS')) {
        // whatever you wanna do here.
    }
}

Find documentation on https://dwij.net/mobile-os-detection-in-php-codeigniter

Ganesh Bhosale
  • 2,000
  • 18
  • 21