Starter help |
[eluser]Unknown[/eluser]
Hi all I am relatively new to code igniter and php although I have coded in perl for many years. I have been following a few really good tutorials online and have managed to build a login page using back end authentication via a MySQL database and sessions. What I am struggling to understand is I want to be able to check on every page if a user is logged in without having to type loads of duplicate code on each page I have searched the web and while there are some great posts by some talented users about this subject I am not experienced enough to understand what they are talking about Some people are saying create a MY_Controller in the libraries directory and within each construct on every page reference this controller. While I have tried to do this I just get errors. Probably because I don't know what I am doing. What I have so far is 1. Login controller which just displays the login screen 2.verify controller which verifies if that user is logged in, using a model to access the db 3. A home page which I have split into 2 parts. A header which contains a menu and a logout button ( I am also passing a data array to this header to pull out the username and last logged in vars to display) these 2 pages are called by the home controller which is called from the verify controller if they are logged in 4. And a body page with just some placeholder text on it What I would like to achieve is a simple way of checking on every new page I call if the user has a valid session and is logged in, if not redirect them back to the login page Any help or suggestions would be very much appreciate Thanks. Mythandra
[eluser]FireMe[/eluser]
would be easier if you post some of you code but here goes nothing. you want something like in you login process model Code: $this->session->set_userdata('logged_user', $user_id); another couple of functions in your model Code: function check_logged(){ then on your controllers autoload your model then simple do this on your controllers Code: if($this->model_name->check_logged()===FALSE) I am new to coding also but this is what i have learned so far hope it helps
[eluser]CroNiX[/eluser]
/application/core/MY_Controller.php (Goes in /application/CORE (because you are extending /system/core/Controller.php), not a Library) Code: class MY_Controller extends CI_Controller { All other controllers that you want to have auth checked should now extend MY_Controller instead of CI_Controller.... Code: class Your_controller extends MY_Controller { Now, whenever you go to this controller, it will first load the MY_Controller and since the auth check is in it's construct, that will be executed every time before continuing on to your actual Your_controller. If you have a controller that you DON'T want auth checked, have that controller extend the regular CI_Controller instead of MY_Controller. You can take this a step further and have several "base controllers" (which is what MY_Controller is). You can have a different one for Front End, Back End, whatever, depending on the needs of your project. Here's a good article on how to take it one step further: http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY |
Welcome Guest, Not a member yet? Register Sign In |