开发者

How can I limit access to specific functions in CodeIgniter?

开发者 https://www.devze.com 2022-12-23 02:38 出处:网络
I have a huge controller in codeigniter, with many functions. I want to limit access to certain functions. How should I proceed?

I have a huge controller in codeigniter, with many functions. I want to limit access to certain functions. How should I proceed?

And can I call the 开发者_JAVA百科functions using cron daemon???

Or should I place those functions in another controller??


  1. a) To limit the access to functions in your controller you shold use private function declaration example:

function _example_function() {...} USING the underscore!!

This way its impossible to call this function by URL.

  1. b) Other simple way to restrict the access to functions in your controller is to use session variables and states to block the access.

2.) Yes you can use cron to run function just call the URL:

http://host/controller_name/FUNCTION

Regards,
Pedro


I have a huge controller in codeigniter, with many functions. I want to limit access to certain functions. How should I proceed?

You can use some access control based on session to limit access to certain controllers->functions only. At the start of the function you can place the code like if($_SESSION['user'] != 'xyz') exit('access denied') ;

And can I call the functions using cron daemon???

yes you can call any controller function in cron with this command wget https://www.example.com/controller-name/function-name

Or should I place those functions in another controller?? Its always a good idea to refactor the code if its getting very big and getting unmanageable.


Another way is to use the protected namespace. When I try calling a function which is marked as protected I'm able to use it within the PHP code yet when trying to load it trough the browser I receive a 404.

Of course marking it as private would work too, but then you'd loose the ability to use the function within an extension of your class. When working with core extensions a lot that would be a problem.

cu Roman

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号