hi im following a tutorial for a system running on PDO and i have encountered a error can anyone check this out

Fatal error: Call to a member function count() on a non-object in /home/matureco/public_html/classes/User.php on line 21

user.php

public function find($user = null) {
        if($user) {
            $field = (is_numeric($user)) ? 'id' : 'username';
            $data = $this->_db->get('users', array($field, '=', $user));

            if($data->count()) {  // this is the one getting the error from
                $this->_data = $data->first();
                return true;
            }

        }
        return false;
    }

    public function login($username = null, $password = null) {
        $user = $this->find($username);
        print_r($this->_data);
        return false;    
    }

login.php

<?php
require_once 'core/init.php';

if(Input::exists()) {
  if(Token::check(Input::get('token'))) {

    $validate = new Validate();
    $validation = $validate->check($_POST, array(
        'username' => array('required' => true),
        'password' => array('required'=> true),
    ));

    if($validation->passed()) {
       $user = new User();
       $login = $user->login(Input::get('username'), Input::get('password'));

       if($login) {
        echo 'Success';
     } else {
        echo '<p>Sorry, logging in failed.</p>';
        }
     } else {   
        foreach($validation->errors() as $error) {
            echo $error, '<br>';
        }
    }

    }
  }
?>

any help would be much appreciated ty x

Recommended Answers

All 7 Replies

Hi! It happens because:

$data = $this->_db->get('users', array($field, '=', $user));

is not returning the object. It could be because the query failed or for something else, sure _db it's a property and not a method? For example:

$data = $this->_db()->get('users', array($field, '=', $user));

What can be returned by the get() method? If there are cases in which your get() can return something different from the object, then you should check it by using is_object():

if(is_object($data) && $data->count())

hi hun ty that sorted that out brilliant just one more thing when i go to login in it comes with login failed although i have registered if you can check why its not logging me in
this is the user.php file

<?php
class User {
    private $_db,
    $_data,
    $_sessionName;

    public function __construct($user = null) {
        $this->_db = DB::getInstance();

        $this->_sessionName = Config::get('session/session_name');
    }

    public function create($fields = array()) {
        if(!$this->_db->insert('users', $fields)) {
            throw new Exception('There was a problem creating an account.');
        }
    }

    public function find($user = null) {
        if($user) {
            $field = (is_numeric($user)) ? 'id' : 'username';
            $data = $this->_db->get('users', array($field, '=', $user));

                if(is_object($data) && $data->count()) {
                $this->_data = $data->first();
                return true;
            }

        }
        return false;
    }

    public function login($username = null, $password = null) {
        $user = $this->find($username);

        if($user) {
            if($this->data()->password === Hash::make($password, $this->data()->salt)) {
                Session::put($this->_sessionName, $this->data()->id);
                return true;
            }
        }
        return false;    
    }

    private function data() {
        return $this->_data;
    }
}
?>

heres the login.php bit

<?php
require_once 'core/init.php';

if(Input::exists()) {
  if(Token::check(Input::get('token'))) {

    $validate = new Validate();
    $validation = $validate->check($_POST, array(
        'username' => array('required' => true),
        'password' => array('required'=> true),
    ));

    if($validation->passed()) {
       $user = new User();
       $login = $user->login(Input::get('username'), Input::get('password'));

       if($login) {
        echo 'Success';
     } else {
        echo '<p>Sorry, logging in failed.</p>';
        }
     } else {   
        foreach($validation->errors() as $error) {
            echo $error, '<br>';
        }
    }

    }
  }
?>

and heres the form submittion button

<td><input type="hidden" name="token" value="<?php echo Token::generate();  ?>"/><input type="submit" value="Login" /></td>

any help would be much appreicated ty x

No error? Sure it's Hash::make() and not Hash::check()?

tried changing Hash::check() to Hash::make() and it still fails ?

If you're using Illuminate Hasher then it should look like:

if(Hash::check($password, $this->data()->password))

rather than your previous IF statement, because the hasher will generate a different result each time, so you cannot compare them directly. This is basically doing:

public function check($value, $hashedValue, array $options = [])
{
    if (strlen($hashedValue) === 0) {
        return false;
    }
    return password_verify($value, $hashedValue);
}

If you're using something different, please point me to the library, so that I can check their documentation.

See:

Hm, lol, ok, it was so similar I made a mistake :D

So, your Hash::make() method should look like this:

public static function make($string, $salt=''){
    return hash('sha256', $string.$salt);
}

With two only arguments in the hash() function, correct? If for some reason you have a comma in between $string and $salt then you would get binary data in raw format, because the hash() function would read $salt as the third argument.

Example:

print hash('sha256', 'test', 'salt');
# outputs
ЁL}e/ZO,]l

print hash('sha256', 'test' . 'salt');
# outputs
4edf07edc95b2fdcbcaf2378fd12d8ac212c2aa6e326c59c3e629be3039d6432

For reference I'm checking the code through this repository, which seems written by one of those participants:

If the above suggestion still doesn't help, please share the code or a repository with your test code, I will not go through the linked tutorial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.