I'm working to implement an users management class inside an already existing project, so I wrote this simple code that will be called from a controller who is responsable to hash the password and will verify if all the required fields are correctly filled from the user. My doubt is on how i can improve the code, I'm thinking about including a mail verification and a simple way to manage the $_SESSION
, but for now I'm focused on the basic functionality of the class.
class.php
class hatdb{
public function __construct(){
if(is_null($this->db)){
$this->db = new PDO('mysql:host=localhost;dbname=hatdb','','');
}
}
}
class users extends hatdb{
public function createUser($email,$username,$password){
$stmt = $this->db->prepare('INSERT INTO users (email,username,password) VALUES (?, ?, ? )');
if($stmt->execute(array($email,$username,$password))){
echo 'Account successful created';
} else {
echo 'Something was wrong during the registration process.';
}
}
public function loginUser($username,$password){
$stmt = $this->db->prepare('SELECT email,username,password FROM users WHERE email = ? OR username = ?');
$stmt->execute(array($username,$username));
if($stmt->rowCount() > 0){
$result = $stmt->fetch(PDO::FETCH_OBJ);
if(password_verify($password, $result->password)){
echo 'logged';
} else {
echo 'wrong password';
}
} else {
echo 'Username or email does not exist in the database.';
}
}