Rahul
Rahul

Reputation: 27

Getting: Fatal error: Call to a member function count() on a non-object

I'm using php 5.3.13. This script is for login remember me system but when I login with "remember me" it's giving me this:

Fatal error: Call to a member function count() on a non-object in C:\wamp\www\oops_login_system\ooplr\classes\User.php

The code is as follows:

login.php

$user = new User();
$remember = (Input::get('remember') === 'on') ? TRUE : FALSE;
$login = $user->login(Input::get('username'), Input::get('password'), $remember);

user.php

public function login($username = null, $password = null, $remember = FALSE) {

    if (!$username && !$password && !$this->exists()) {
        Session::put($this->_sessionName, $this->data()->id);
    } else {
        $user = $this->find($username);
        if ($user) {
            if ($this->data()->password === Hash::make($password, $this->data()->salt)) {
                Session::put($this->_sessionName, $this->data()->id);
                if ($remember) {
                    $hash = Hash::unique();
                    $hashCheck = $this->_db->get('user_session', array('user_id', '=', $this->data()->id));
                    if (!$hashCheck->count()) {
                        $this->_db->insert('users_session', array(
                            'user_id' => $this->data()->id,
                            'hash' => $hash
                        ));
                    } else {
                        $hash = $hashCheck->first()->hash;
                    }

                    Cookie::put($this->_cookieName, $hash, config::get('remember/cookie_expiry'));
                }
                return TRUE;
            }
        }
    }
    return false;
}

Upvotes: 0

Views: 764

Answers (3)

Lodi Cronje
Lodi Cronje

Reputation: 11

Hi check the following in your code. $hashCheck = $this->_db->get('user_session', array('user_id', '=', $this->data()->id));

It should be $hashCheck = $this->_db->get('users_session', xxxxxxxxx

The count looks in users_session table but u are trying to get a table that does not exist.

Well thats how i understand it.

Upvotes: 1

Mehdi
Mehdi

Reputation: 659

try

count($hashCheck)

indesd of

$hashCheck->count()

if does not worked, so $hashCheck is null, then chage the line

if (!$hashCheck->count()) {

to

if ($hashCheck && $hashCheck->count() > 0){

Upvotes: 0

Michal Trojanowski
Michal Trojanowski

Reputation: 12322

In this line:

 $hashCheck = $this->_db->get('user_session', array('user_id', '=', $this->data()->id));

you are getting something which not an object, so you can't call $hashCheck->count() in the next line. You should what exactly do you get and why (is this a bug or planned behaviour).

Upvotes: 1

Related Questions