RalphLeMouf Posted June 26, 2012 Share Posted June 26, 2012 Hello - I am fairly new to code igniter and have unfortunately hit a wall with the simple task of logging in a user to my db. I have done plenty of research on the cause of the error and it seems like it's a connection to my db issue ( which I've used troubleshooting methods on that scenario) I have tried re-making the login area with a more manual approach ( no helpers or auto loads ) and still am getting the same error for my model: Call to a member function where() on a non-object in /Users/my_computer/Sites/CodeIgniter/application/models/user_model.php on line 7 Here is my model: <?php class User_model extends CI_Model { function validate () { $this->db->where('email', $this->input->post('email')); $this->db->where('password', sha1($this->input->post('password'))); $this->db->get('users'); if($query->num_rows == 1) { return true; } } } ?> View: <title>Login</title> <!--MAKE SURE SIGNED OUT HEADER IS IMPLEMENTED FOR ALL SIGNED OUT PAGES INCLUDING THIS ONE--> <div class="structure clearfix"> <h1 class="title_header"> Sign In </h1> <div id="signin_form"> <?php echo validation_errors(); echo form_open('auth/validate_credentials'); echo "<div class='form_text_signin'>"; echo "Email"; echo "</div>"; echo form_input('email'); echo "<div class='form_text_signin'>"; echo "Password"; echo "</div>"; echo form_input('password'); echo form_submit('submit', 'Submit'); echo form_close(); ?> </div> </div> Controller: <?php class Auth extends CI_Controller { // this is automatically called if no other function is called // it simply turns around and calls the login() function to show the login page public function index() { $this->login(); } public function login() { $data['main_content'] = 'auth/login_form'; $this->load->view('includes/templates/main_page_template', $data); } function validate_credentials () { $this->load->model('user_model'); $this->user_model->validate(); if($query) { $data = array( 'email' => $this->input->post('email'), 'password' => $this->input->post('password'), 'is_logged_in' => true ); $this->session->set_userdata($data); redirect('account/dashboard'); } else { $this->index(); } } } ?> Additionally here is the relevant information from the autoload.php file: $autoload['libraries'] = array('database', 'session'); $autoload['helper'] = array('url', 'form'); as well as from my database.php file: $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = 'root'; $db['default']['database'] = 'my_site_db'; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; thanks so much in advance. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 26, 2012 Share Posted June 26, 2012 Sounds like the CI_Model class isn't setting up $db as/when you expect it to. Look into that. Quote Link to comment Share on other sites More sharing options...
RalphLeMouf Posted June 26, 2012 Author Share Posted June 26, 2012 Yeah that's what seems to be the issue,however I have tried loading it manually and with the autohelper function and it's still posing that error. Do you happen to have another suggestion or is there something I am blatantly missing? Quote Link to comment Share on other sites More sharing options...
RalphLeMouf Posted June 27, 2012 Author Share Posted June 27, 2012 Yeah guy's I'm stuck. I've tried so many different things and am just clueless as to why it's STILL posing this error. For some reason it's not connecting to my db. This is what I currently have for the model ( which is the page posing the error and disabling the page from running the validate_credentials function <?php class User_model extends CI_Model { public function __construct() { parent::__construct(); $this->load->database(); } function validate () { $this->db->select('*'); $this->db->from('users'); $this->db->where('email', $this->input->post('email')); $this->db->where('password', sha1($this->input->post('password'))); $validate_user = $this->db->get(); if($validate_user->num_rows == 1) { return TRUE; } } } Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 27, 2012 Share Posted June 27, 2012 $this->load->database(); Look there. Quote Link to comment Share on other sites More sharing options...
RalphLeMouf Posted June 27, 2012 Author Share Posted June 27, 2012 trust me, I have been the whole time for the most part. I've tried: parent::__construct(); $this->load->database('cysticlife_CI'); parent::__construct(); $db = $this->load->database(); to name a couple Quote Link to comment Share on other sites More sharing options...
RalphLeMouf Posted June 27, 2012 Author Share Posted June 27, 2012 I realize that is where the issue is, I'm just clueless apparently on how to fix it!!! Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 28, 2012 Share Posted June 28, 2012 So what that says is load is an object with a method of database(). What is in that method? Quote Link to comment Share on other sites More sharing options...
RalphLeMouf Posted June 28, 2012 Author Share Posted June 28, 2012 I'm not sure what you mean? Can you please be more detailed if possible? thanks for the help Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 28, 2012 Share Posted June 28, 2012 Honestly I think this should go in third party since it's Code Igniter. Since I don't know CI, I can only guess. You need to look in the code for CI_Model and see how the database gets loaded. Something is not working there. Your code is expecting db to be an object and it's not, so you need to find the code that creates that $db variable. Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted June 28, 2012 Share Posted June 28, 2012 You are not passing the params to the function. This is my login controller method + model controllers/account <?php // -------------------------------------------------------------------- function login() { // Check to see if the user is logged in if ($this->user->logged_in()) { // User is logged in, redirect them redirect('account'); } // Retrieve login submit from account/login $login = $this->input->post('login'); // Login comes from navbar if( !$login ) { // Retrieve the forms $redirect = $this->input->post('redirect'); // Set form validation rules $this->form_validation->set_rules('login_email', 'Email', 'trim|required|callback__check_login'); $this->form_validation->set_rules('login_pass', 'Password', 'trim|required'); // Check for timeout if( $this->session->userdata('timeout') ) $this->form_validation->set_rules('captcha', 'Image Verification', 'trim|required|callback__check_captcha'); // Form validation passed, so continue if (!$this->form_validation->run() == FALSE) { // Login the user if(!$this->user->login($this->input->post('login_email'), $this->input->post('login_pass'), $this->input->post('remember'))) { // Login failed, alert the user $this->form_validation->set_message('login_failed', 'Invalid credentials'); } else { // Redirect the user redirect($redirect); } } // Load the login view $this->load->view(SITE . 'login'); } models/user_model <?php // -------------------------------------------------------------------- function login($email = '', $password = '', $remember = '') { // Check to see if we have valid data if($email == '' OR $password == '') { // Data is invalid, return FALSE return FALSE; } // Create the password $password = ** encryption method // Setup the data $data = array( 'email' => $email, 'password' => $password, 'user_activation' => '1' ); // Retrieve the user $user = $this->users->get_user($data); // Check if query exists if($user) { // Check if the user wants to be remembered if(isset($remember)) { // Remember the user set_cookie('email', $this->encrypt->sha1($this->input->post('login_email')), '31536000'); set_cookie('password', $password, '31536000'); set_cookie('username', $user->username, '31536000'); } // User exists, create the session data, return TRUE $this->session->set_userdata('user_id', $user->user_id); $this->session->set_userdata('username', $user->username); $this->session->set_userdata('email', $user->email); // Clear timeout, if it exsits $this->session->unset_userdata('timeout'); return TRUE; } else { // User doesn't exist, return FALSE return FALSE; } } Quote Link to comment Share on other sites More sharing options...
RalphLeMouf Posted June 28, 2012 Author Share Posted June 28, 2012 First off...thanks so much for the help I tried to adapt your model and controller to mine and am getting this error: Call to a member function logged_in() on a non-object in /Users/my_computer/Sites/CodeIgniter/application/controllers/auth.php on line 16 Here is my controller post adaptation: <?php class Auth extends CI_Controller { // this is automatically called if no other function is called // it simply turns around and calls the login() function to show the login page public function index() { $this->login(); } function login() { // Check to see if the user is logged in if ($this->user->logged_in()) { // User is logged in, redirect them redirect('dashboard'); } // Retrieve login submit from account/login $login = $this->input->post('login'); // Login comes from navbar if( !$login ) { // Retrieve the forms $redirect = $this->input->post('redirect'); // Set form validation rules $this->form_validation->set_rules('email', 'Email', 'trim|required|callback__check_login'); $this->form_validation->set_rules('password', 'Password', 'trim|required'); // Check for timeout if( $this->session->userdata('timeout') ) $this->form_validation->set_rules('captcha', 'Image Verification', 'trim|required|callback__check_captcha'); // Form validation passed, so continue if (!$this->form_validation->run() == FALSE) { // Login the user if(!$this->user->login($this->input->post('email'), $this->input->post('password'), $this->input->post('remember'))) { // Login failed, alert the user $this->form_validation->set_message('login_failed', 'Invalid credentials'); } else { // Redirect the user redirect($redirect); } } // Load the login view $this->load->view('auth/login'); } function __construct() { parent::__construct(); $this->load->model('user_model'); } } } Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted June 29, 2012 Share Posted June 29, 2012 My logged_in() function is a simple session creator that occurs when a user logs in successfully that is stored in an autoloaded model so it is always available. Quote Link to comment Share on other sites More sharing options...
RalphLeMouf Posted June 29, 2012 Author Share Posted June 29, 2012 I see and understand that, but am not at all understanding why it's posing an error. None the less an error very close to the one that originally got me to post this thread. Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted June 29, 2012 Share Posted June 29, 2012 It's not an object because it is not initialized - because you haven't implemented the function anywhere that is loaded. Quote Link to comment Share on other sites More sharing options...
RalphLeMouf Posted June 29, 2012 Author Share Posted June 29, 2012 So your saying I have to create the $user->logged_in() somewhere before. Quote Link to comment Share on other sites More sharing options...
RalphLeMouf Posted June 29, 2012 Author Share Posted June 29, 2012 I'm actually going to read into creating class libraries so if I have trouble after that I'll repsond. thanks for al your help so far! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.