There’s user on a different forum (I’m not going to name) that is can be a little rude at times, but he does have a lot of valid points.
Anyways, he says a user’s stored password should never be pulled out of the MySQL database table and that password_hash and password_verify are not secure. He gave an example and I modified it a little to fit my coding style.
Here’s my version of it:
Create:
[php] public function create($data) {
if (is_array($data)) { // If statement probably not needed:
$db = DB::getInstance();
$pdo = $db->getConnection();
/* Secure the Password by hashing the user’s password. */
$data[‘password’] = hash(‘whirlpool’,$data[‘password’]);
try {
/* Set the query variable */
$this->query = 'INSERT INTO users (username, password, full_name, email, confirmation_code, security_level, private, date_added) VALUES (:username, :password, :full_name, :email, :confirmation_code, :security_level, :private, NOW())';
/* Prepare the query */
$this->stmt = $pdo->prepare($this->query);
/* Execute the query with the stored prepared values */
$this->result = $this->stmt->execute([
':username' => $data['username'],
':password' => $data['password'],
':full_name' => $data['full_name'],
':email' => $data['email'],
':confirmation_code' => $data['confirmation_code'],
':security_level' => $data['security_level'],
':private' => $data['private']
]); // End of execution:
return \TRUE;
} catch (PDOException $error) {
// Check to see if name is already exists:
// This will be only good a really really busy websites:
$errorCode = $error->errorInfo[1];
if ($errorCode == MYSQL_ERROR_DUPLICATE_ENTRY) {
error_log("Duplicate Name was Enter", 1, "[email protected]");
} else {
throw $error;
}
}
} // End of main if-statement:
}[/php]
Read:
[php] public function read($username, $password) {
$db = DB::getInstance();
$pdo = $db->getConnection();
/* Setup the Query for reading in login data from database table */
$this->query = ‘SELECT id FROM users WHERE username=:username AND password=:password’;
$this->stmt = $pdo->prepare($this->query); // Prepare the query:
$this->stmt->execute([
':username' => $username,
':password' => hash('whirlpool', $password)
]); // Execute the query with the supplied user's parameter(s):
$this->stmt->setFetchMode(PDO::FETCH_OBJ);
unset($password);
if ($this->user_id = $this->stmt->fetchColumn()) {
return $this->user_id;
} else {
return FALSE;
}
}[/php]
I haven’t fully vetted this yet, I think adding a salt to the password would make it secure and I’m still redoing my own PHP registration script. Personally I think password_hash and password_verify are good enough and that is the reason I haven’t fully been sold on that part. Though I agree not pulling the password for the database table is good practice. Once I have it tested and have a fully blown login/registration script(s) I will post it here somewhere.