Welcome Guest, Not a member yet? Register   Sign In
Nettuts Day 6- Login Issues
#1

[eluser]domoench[/eluser]
Hi All,

I'm new to CodeIgniter, and I'm stuck in the middle of the Nettuts CodeIgniter Day 6 tutorial. At 30:10 I am no longer able to do what is shown.

My login.php controller is as follows:
Code:
<?php

class Login extends CI_Controller
{
    function index()
    {
        $data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);
    }
    
    function validate_credentials()
    {
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();
            
        if($query) // If user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            
            $this->session->set_userdata($data);
            redirect('site/members_area');
        }
        
        else
        {
            $this->index();
        }
    }
}

I guess something in the body of validate_credentials()'s if statement is not working, because the else statement is always executed. I suspect that $this->membership_model->validate(); (membership_model.php shown below) is not returning true for the if statement.

membership_model.php
Code:
class Membership_model extends CI_Model
{
    function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('membership');

        if($query->num_rows == 1)
        {
            return true;
        }
    }
}

While trying to figure out the issue I've done the following:
- Double-checked that my username and password in my database matched the language in the above membership_model.php
- Autoloaded the session class, and put a string of 32 assorted characters into the $config['encryption_key'] within the config.php file.
- Looked for syntactical typos

Any suggestions as to what the problem might be? Am I handling the session class/encryption key correctly?

Thanks!
David
#2

[eluser]InsiteFX[/eluser]
Try this:
Code:
class Membership_model extends CI_Model
{
    function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('membership');

        if($query->num_rows() == 1)  // missing the ()
        {
            return true;
        }
    }
}

InsiteFX
#3

[eluser]domoench[/eluser]
Thanks for catching that InsiteFX - I guess there's more to it though, after correcting that typo there's no change in behavior.
#4

[eluser]InsiteFX[/eluser]
I' ll download it later and take a look at it!

There is not enough code here to see what is really going on!

InsiteFX
#5

[eluser]domoench[/eluser]
I created a test view, and passed it the data from my 'membership' table (contains info about members that can log in: first_name, last_name, username, password), successfully outputting it to the screen. So I know I can connect to and pass around info from the database and table, and that they look like I expected them to.

Could the problem involve the password encryption md5() function in membership_model.php? The tutorial video is demonstrated on CI 1.7, and I'm currently working with 2.0.1, and I already discovered one difference between the versions- In 2.0.1 autoloading the session class requires you set an encryption key. So something related to encryption is different now. I assigned a random 32 character string to the 'encryption_key' which allowed me to proceed with autoloading the session class- but perhaps I did something incorrectly there?




Theme © iAndrew 2016 - Forum software by © MyBB