CodeIgniter Forums
redirect based on user agent - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: redirect based on user agent (/thread-33662.html)



redirect based on user agent - El Forum - 09-03-2010

[eluser]dan.syme[/eluser]
Where can I place a redirect to https based on user agent with out having to put the user agent check and redirect into every controller?


redirect based on user agent - El Forum - 09-04-2010

[eluser]slowgary[/eluser]
Hi Dan,

In "system/application/config/config.php" you'll find this line:
Code:
$config['subclass_prefix'] = 'MY_';

What this means is that CodeIgniter will automatically load files from your library directory (application/libraries) that are named after CodeIgniter's native libraries if they have that prefix.

So for example (an example that will help you achieve your goal), you can create a file in application/libraries called MY_Controller.php. Then in that file make a class that extends the Controller class and does what you need it to do, like so:
Code:
<?php
/*
* File: system/application/libraries/MY_Controller.php
*/

class BaseController extends Controller
{
     function BaseController()
     {
          parent::Controller();

          $this->load->library('user_agent');
          if($this->agent->is_mobile())
          {
               if($_SERVER['HTTPS'] != 'on')
               {
                    $this->load->helper('url');
                    redirect(str_replace('http://', 'https://', current_url()));
               }
          }
     }
}

Now in all of your site's controllers you'll just extend the BaseController instead of Controller and you're all set.

I hope this helps.