The following files exists in this folder. Click to view.
register.class.php63 lines UTF-8 Unix (LF) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
<?php
class Register{
private $username;
private $raw_password;
private $enc_password;
public $error;
public $success;
private $storage = "data.json";
private $stored_users;
private $new_user;
public function __construct($username, $password){
$this->username = trim($username);
$this->username = filter_var($this->username, FILTER_SANITIZE_STRING);
$this->raw_password = filter_var(trim($password), FILTER_SANITIZE_STRING);
$this->enc_password = password_hash($this->raw_password, PASSWORD_DEFAULT);
$this->stored_users = json_decode(file_get_contents($this->storage), true);
$this->new_user = [
"username" => $this->username,
"password" => $this->enc_password,
"balance" => array(),
];
if($this->checkFieldValues()){
$this->insertUser();
}
}
public function checkFieldValues(){
if(empty($this->username) || empty($this->raw_password)){
$this->error = "Bägge fälten behöver fyllas i";
return false;
}else{
return true;
}
}
private function usernameExists(){
foreach($this->stored_users as $user){
if($this->username == $user["username"]){
$this->error = "Användarnamnet finns redan, var god välj ett annat.";
return true;
}
}
return false;
}
private function insertUser(){
if($this->usernameExists() == FALSE){
array_push($this->stored_users, $this->new_user);
if(file_put_contents($this->storage, json_encode($this->stored_users, JSON_PRETTY_PRINT))){
return $this->success = "Kontot har skapats.";
}else{
return $this->error = "Något gick åt helvete. Försök igen.";
}
}
}
}
?>