代码不回应“ok”

时间:2014-03-01 01:32:53

标签: php database login hash

我一直在寻找年龄而且我再次陷入困境,当我为与数据库中的密码匹配的用户输入正确的密码时,我的代码没有回应“ok”。

其他一切都很好,我不确定我是否错过了任何愚蠢的事情;我可能有,为愚蠢道歉:)

PHP:

class User {
    private $_db, $data;

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

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

    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->_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)) {
                echo 'ok';
            }
        }

        return false;
    }

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

2 个答案:

答案 0 :(得分:1)

在你的login()上,你必须像这样重写......

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

        $user = $this->find($username);


        if($user) {
            if($this->data()->password === Hash::make($password, $this->data()->salt)) {
                //echo 'ok';
                return true;   
            }

        }
        else { return false; }
    }

注释掉echo并从那里返回true。所以你将从你的实例中调用你的函数,比如

if($someinst->find('someuser'))
      {
         echo "User Found";
      }
else { echo "Not Found"; }

答案 1 :(得分:1)

尝试这样来解决您的问题:

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

    if($user) {
        if($this->data()->password === Hash::make($password, $this->data()->salt)) {
            echo 'User and password Ok!';            
        } else {
            echo 'User and password do not match'; // Handle it
        }
    } else {
        echo 'User not found'; // Handle it
    }
    return false;
}

此外,在第一种观点,这可能是问题:

$this->data()->password

也许你应该使用类似的东西:

if($user->password === Hash::make($password ...