Warning: Illegal string offset 'Username' in /home/runner/Chatroom/register.class.php on line 37

Warning : Illegal string offset ‘Username’ in /home/runner/Chatroom/register.class.php on line 37

Repl link:](https://replit.com/@djrwls/Chatroom)

class RegisterUser{
   // class properties.
   private $username;
   private $raw_password;
   private $encrypted_password;
   public $error;
   public $success;
   private $storage = "users.json";
   private $stored_users; // array
   private $new_user; // array

    public function __construct($username, $password){
      $this->username = filter_var(trim($username), FILTER_SANITIZE_STRING);
      $this->raw_password = filter_var(trim($password), FILTER_SANITIZE_STRING);
      $this->encrypted_password = password_hash($password, PASSWORD_DEFAULT);
      $this->stored_users = json_decode(file_get_contents($this->storage), true);
      $this->new_user = [
         "Username" => $this->username,
         "Password" => $this->encrypted_password,
    private function checkFieldValues(){
      if(empty($this->username) || empty($this->raw_password)){
         $this->error = "Both fields are required.";
         return false;
         return true;

    private function usernameExists(){
      foreach ($this->stored_users as $user) {
         if($this->username == $user['Username']){
            $this->error = "Username already taken, please choose a different one.";
            return true;
    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))){
            return $this->success = "Your registration was successful";
            return $this->error = "Something went wrong, please try again";
1 Like

Best I can tell, $user is an associative array from the JSON file users.json. On line 37 in register.class.php, from what I understand, you check if the username being used to create a new account is equal to the Username property in the $user associate array, but this property doesn’t exist in the users.json file. I think you need to better organise how you’re storing users credentials because some users are stored as "<username>": <password> while some are stored as "<id>": { "username": <username>, "password": <password> }. To fix your issue, I would recommend first standardising how you are storing which user is which, because currently not all users have a username property which is what I believe is causing the error.

We did figure out how to!

1 Like