【问题标题】:Code not echoing "ok"代码没有回显“ok”
【发布时间】:2014-04-02 06:26:47
【问题描述】:

我一直在寻找年龄,但我又被卡住了,当我为与数据库中密码匹配的用户输入正确的密码时,我的代码没有回显“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;
    }
}

【问题讨论】:

  • 你检查你的 PHP 日志了吗?
  • 您似乎使用不同的哈希函数进行注册和登录,或者您的凭据毕竟是错误的。

标签: php database login hash


【解决方案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"; }

【讨论】:

    【解决方案2】:

    试试这个来解决你的问题:

    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 ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      相关资源
      最近更新 更多