LoginSignup
8
8

More than 5 years have passed since last update.

かつお君へ PHPのpashword_hashでログイン処理を作成

Posted at

かつお君へ PHPのcryptでログイン処理を作成にて指摘を頂いたので、内容を再度まとめました。
以下を使うとSALTを固定値で指定しなくてもハッシュを作成出来ます。

ユーザ作成画面にて、入力されたパスワードにpassword_hash()を使います。
返り値のhashをpassとして保存します。
以前cryptのCRYPT_BLOWFISHでパスワードを生成していたので、アルゴリズムはPASSWORD_BCRYPTを指定してみました。

App/Controller/User.php
public function registUser() {
    if ($this->request->is('post'))
    {
        $this->loadModel('User');
        $this->request->data['User']['pass'] = password_hash($this->request->data['User']['pass'], PASSWORD_BCRYPT);
        $this->User->save($this->request->data);
    }
}

照合の際は、ユーザーIDでデータをSELECTし、password_verify()に先ほどのhashとログイン時に入力されたpassを渡すとbooleanが返ってきます。

App/Model/User.php

public function checkUserLogin($id, $pass) {
    $user = $this->find('first', array('conditions' => array('id' => $id)));
    return password_verify($pass, $guide['User']['pass']);
}
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8