Welcome Guest, Not a member yet? Register   Sign In
Member area..
#11

[eluser]pstp[/eluser]
So, I tried to input the id to session, and get some wrong.

This is my Controller code:
Code:
function validate_credentials()
    {
        $this->load->model('user_model');
        $data = $this->user_model->validate(); //var querymemanggil method validate di user_model
        
        if($data) //if the user's credentials validated...
        {
            $new_data = array(
            'email' => $this->input->post('email'),
            'id' => $data->id_user,
            'is_logged_in' => TRUE,
            );
            $this->session->set_userdata($new_data);
            redirect('home');
            
        }

else{......}

and my Model:
Code:
function validate()
{
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', md5($this->input->post('password')));
        $this->db->where('activated', 1);
        $query=$this->db->get('user');
    
        return $data = ($query->num_rows() == 1) ? $query->row() : TRUE;
}


Nah, I've done that. And I get right for the right user. It's directed to the home page, and succeed on inputting data, especially id_user to session.

But, if in login_form the user inputted the wrong email and or password, I got this stupid message:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/login.php

Line Number: 19

ps: line number 19 is referred to 'id' => $data->id_user

I called it stupid 'cause I'm not sure what's happening here. So, master, please help me..
#12

[eluser]LuckyFella73[/eluser]
If user and password are right your model returns an array ($data).
If not then the model returns "true", what doesn't make much sense ..

That is why this line can't work when no row is returned:
Code:
'id' => $data->id_user,

Better try something like this:
Code:
// model part:
function validate()
{
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', md5($this->input->post('password')));
        $this->db->where('activated', 1);
        $query = $this->db->get('user');
    
    if ($query->num_rows() == 1)
    {
        $data = $query->row();
        return $data;
    }
    else
    {
        return FALSE;
    }
}



// controller part:
if($data == TRUE) //if the user's credentials validated...
        {
            $new_data = array(
            'email' => $this->input->post('email'),
            'id' => $data->id_user,
            'is_logged_in' => TRUE,
            );
            $this->session->set_userdata($new_data);
            redirect('home');
            
        }

else{ // ...}
#13

[eluser]pstp[/eluser]
Oh my god! it works! I spent my 6 hours, and the problem is RETURN can only RETURN one thing, isn't it? I mean there only one value that being returned by function (in this case IF statement).

Domou Arigato minna..:-)
#14

[eluser]Bramme[/eluser]
If you would like to understand more about user authentication, I also wrote a tutorial about it a while back. It's kinda outdated atm (written in CI 1.6.x), but it uses some basic concepts.

http://www.bramme.net/2008/07/auth-libra...-tutorial/
#15

[eluser]pstp[/eluser]
Thanks Bramme, but I think it's too early for me to make such a library, though by copying and understanding your tutorial. Is it possible if we just write them down on Controller?

I read much about auth library (mostly by reading the code and the comment), but because I usually just use the library, so I never know how they work. What exactly library is? Bunch of functions that any controller/model can call, so instead of writing again the code with the same function, we just write them once and call them in many places. Is it how Lib works? But I'm not sure yet to create one, it's a lil bit different with function in controller, makes me afraid to try..Sad
#16

[eluser]pstp[/eluser]
Hi again everyone..Big Grin

Nah, Brame, I should thank once again. Your tutorial is exactly match for `newbie summer student`, and everyone here should be happy, 'cause I start to make a Library! yea!!

And again, I got a problem with restriction. If only a user with the right username and password can enter the member area, I've done the function. But I'm confused with the business process of restricting someone with some information. For example the application have 3 kinds of member: admin, super_user and common_user. Page for admin is definitely different with page for the others. But the super_user's and common_user's page should basically be the same, of course the url page for both must be the same (only id makes them different). In this case, I want to ask:
1. Is it on view I should give the restriction function? Because I want to filter the content for common_user, and I don't have any thought that it's should be on Controller.
2. If on the view, does it mean I should do it procedurally? Is there no way to make it more light? for example, this user_view.php for super_user and common_user:

If(restriction function=true)
{
this is content for super_user;
}
else
{
this is content for common_user;
}

note: content for common_user is actually is part of content for super_user, that's why I think it's waste to type the code again and again, hehe..

Somebody who read this, hehe, I need your help...Wink




Theme © iAndrew 2016 - Forum software by © MyBB