Welcome Guest, Not a member yet? Register   Sign In
Slight session problem
#1

[eluser]eokorie[/eluser]
I am trying to write my own user authentication code. So far everything is going well, I can get the user to login but it refuses to save the username and email address into the session data. However, the user id and logged in status are saved. Hope the code below explains what I am trying to acheive. The code below is in my user model.

Code:
function login_user($username, $password)
        {
            $password = md5($password);
            $this->db->select('id_usr','email_usr','name_usr','password_usr');
            $this->db->where('name_usr',$username);
            $this->db->where('password_usr',$password);
            $this->db->limit(1);
            $query = $this->db->get('gk_users');
            
            $result = $query->row();
            
            if ($query->num_rows() > 0) :
                $logged_user_data = array(
                    'gk_login_id'         => $result->id_usr,
                'gk_login_email'         => $result->email_usr,
                               'gk_login_user'        => $result->name_usr,
                               'logged_in'             => TRUE
                );
                $this->session->set_userdata($logged_user_data);
                return true;
            else:
                return false;      
            endif;
        }

With way the code is just now, when I try to log in, these are the errors I get:

--------------------------------------------------------------

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$email_usr

Filename: models/admin_model.php

Line Number: 23
--------------------------------------------------------------

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$name_usr

Filename: models/admin_model.php

Line Number: 24
--------------------------------------------------------------

If I comment out line 23 and 24, the login process works fine and the session value of id_usr is displayed. Anyone have an idea why this is not working with email_usr and name _usr?

Thanks
#2

[eluser]darkhouse[/eluser]
Well, the errors are telling you that email_usr and name_usr do not exist in the $result object, so it's not a session issue. I'm guessing it's a database issue, but what's weird is that you don't get a database error on that select statement, so maybe it's not a database issue. I think this is a case where some good old fashioned troubleshooting is required. Just start echoing and print_r/var_dump'ing things out. Like, I would do var_dump($result) somewhere to make sure that it contains all the data that it should. If it's not, work backwards and find out why.
#3

[eluser]ecigraeme[/eluser]
Is this a valid way to use select()

$this->db->select('id_usr','email_usr','name_usr','password_usr');

if you comment out that line does it work?

try
$this->db->select('id_usr,email_usr,name_usr,password_usr');
#4

[eluser]eokorie[/eluser]
[quote author="ecigraeme" date="1249407233"]Is this a valid way to use select()

$this->db->select('id_usr','email_usr','name_usr','password_usr');

if you comment out that line does it work?[/quote]

Thanks ecigraeme, that seems to have fixed the issue... but any particular reason why it did not work with just selecting the fields?
#5

[eluser]eokorie[/eluser]
Actually the mistake was mine, should have selected the fields within the quotes instead of separating them

should have been $this->db->select(‘id_usr,email_usr,name_usr,password_usr’);

instead of

$this->db->select(‘id_usr’,‘email_usr’,‘name_usr’,‘password_usr’);

Serves me right for working late at nite... Thanks for the help! Smile
#6

[eluser]darkhouse[/eluser]
Ah, good catch, I missed that completely. That explains why the id was working, but nothing else.




Theme © iAndrew 2016 - Forum software by © MyBB