how do I prevent users from accessing the program through url, and have to login before?, I have the login system ready |
[eluser]Smalbach[/eluser]
Greetings, I'm new programming in php and codeignier. I'm working with my application and I have many controllers example: www.mysite.com/dashboard www.mysite.com/proyects www.mysite.com/proyects/new www.mysite.com/proyects/edit www.mysite.com/etc I need that the user can not access the url, the user must be logged some example of how to do it?
[eluser]jojo777[/eluser]
Hi! Well its pretty common create a function named '_is_logged_in' where you check if the session data 'is_logged_in' or whatever you called it is TRUE. So for example if you dont want people get in www.mysite.com/proyects unless ther are logged do something like this: Code: function proyects(){ So now lets take a look at what _is_logged_in does. It returns TRUE/FALSE depending on the session data. Code: function _is_logged_in(){ Hope it helps you!
[eluser]alexwenzel[/eluser]
If you have a lot of controllers it is better practice to implement the login check within the MY_Controller.php or within codeigniter hooks. The advantage is, that you can maintain your code more easily and you dont need to add the login cheking method to all your controllers.
[eluser]Smalbach[/eluser]
thanks to respond my answer, alexwenzel, do you have some example to do it?,i have a lot of controller and i do not want to do it in all them...
[eluser]jojo777[/eluser]
You can use the code I posted before, typing it in My_Controller.php I use it always. Remember to set all your controllers like this Code: class anycontroller extends MY_Controller { So from any controller you can call that function like this: Code: ... And depends on this return send users to the correct view. EDIT: Even you can put that call in your constructor inside My_Controller so always a controler is load it verify if the user is loged in.
[eluser]alexwenzel[/eluser]
#1 method with hooks: Check out this page to fully understand hooks. http://ellislab.com/codeigniter/user-gui...hooks.html My suggested hook type would be: post_controller_constructor Code: /** #2 method with MY_Controller.php: Create file application/core/MY_Controller.php Code: class MY_Controller extends CI_Controller { All you need to do now is, extending all controllers from All examples above are just typed from out of my mind, dont expect them to work. But the basic idea behind is often used by myself and should work.
[eluser]Mr. Pickle[/eluser]
Check this site for a simple explaination of how to do this with base controllers: http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY It's what @alexwenzel also mentioned with his 2nd option, this link goes further into details and explains also how to seperate admin sections from public sections.
|
Welcome Guest, Not a member yet? Register Sign In |