Files
mallardnet/lib/classes/Register.php
2025-11-22 23:57:26 -08:00

64 lines
2.3 KiB
PHP

<?php
class Register {
private $username;
private $password;
private $conn;
public $error;
public function __construct($username, $password, $conn) {
$this->username = trim($username);
$this->password = trim($password);
$this->conn = $conn;
}
public function checkUsername() {
switch(true) {
case empty($this->username):
$this->error = "Please fill out all of the fields";
break;
case preg_match("/[^a-z0-9 ]/i", $this->username):
$this->error = "Username cannot have any special characters.";
break;
case strlen($this->username) > 16:
$this->error = "Username cannot be longer than 16 characters.";
break;
case strlen($this->username) < 3:
$this->error = "Your username cannot be shorter than 3 characters.";
break;
default:
$stmt = $this->conn->prepare("SELECT username FROM accounts WHERE username = :username");
$stmt->bindParam(":username", $this->username);
$stmt->execute();
if ($stmt->rowCount() > 0)
$this->error = "Username has been already taken.";
}
return $this->error;
}
public function checkPassword($passwordConfirm) {
if (empty($this->password)) {
$this->error = "Please fill out all of the fields.";
} else {
if ($this->password != $passwordConfirm)
$this->error = "Passwords don't match.";
}
return $this->error;
}
public function insertUser() {
$hashed_password = password_hash($this->password, PASSWORD_BCRYPT);
$stmt = $this->conn->prepare("INSERT INTO accounts (username, password) VALUES (:username, :password)");
$stmt->bindParam(":username", $this->username);
$stmt->bindParam(":password", $hashed_password);
$stmt->execute();
session_regenerate_id(true);
$_SESSION["user"] = $this->username;
return true;
}
}
?>