CodeIgniter Forums
Restrict access to MY_Controller functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Restrict access to MY_Controller functions (/thread-33027.html)



Restrict access to MY_Controller functions - El Forum - 08-13-2010

[eluser]dynZack[/eluser]
How do you restrict access in certain functions defined within MY_Controller?

For example:

Code:
class  MX_Controller  extends  Controller  
{
    
    function MX_Controller()
    {
        parent::Controller();
    
    }
      
      
    function set_cookies($params = array())
    {
        ...
    }

}

Going to http://www.example.com/set_cookies outputs a blank page and not a 403 error.

How to make sure that access is forbidden, output a message and redirect to home page?


Restrict access to MY_Controller functions - El Forum - 08-13-2010

[eluser]mddd[/eluser]
If you want a controller method to never be available to the public (that is: you can only call it from within your own code) you can start the name with a _. CI will not make the method available to be called from the URI.
Note: this has nothing to do with being in a controller or in MY_Controller.

Like this:
Code:
function _set_cookies()
{
  // this code can NOT be called like www.example.com/_set_cookies.
}

function other_function()
{
  // this can be called through www.example.com/other_function
  // and from here you can call your other method
  $this->_set_cookies()
}



Restrict access to MY_Controller functions - El Forum - 08-13-2010

[eluser]dynZack[/eluser]
Thanks for the quick answer!