0

I have a project based on PHP with OOP structure. When I register a user, it creates a user object and writes that object to json file. The problem is, in this way each time I create a user, the json object stores separately in the file.(shown in the image)

enter image description here

So when I re-read this json file using json_decode() function, it doesn't read anything. I've used the following method to check the file reading.

    $data = json_decode(file_get_contents($file), true);
    $f = fopen("log", 'a');
    fwrite($f, $data);

So how can I append the json objects as list of arrays(upon registering a single user) or read all the entries from the file?

Most of the solutions that I have seen are predefined arrays, but in my case the objects are being created at runtime and only one at a time.

  • 2
    It probably doesn't read anything because it's not valid json. You have multiple different valid json strings in the file, but the entire file itself is not valid json. You would have to either create a separate json file for each user, or you could store your json objects inside a json object by reading the entire file to an array every time you create a user, add the new object to that array, then save that entire array to the file. – GrumpyCrouton Feb 22 '22 at 15:37
  • 1
    You really should probably be using a database for this instead of json files, because it would be a lot easier and more secure. Also, it's not secure to store plaintext passwords, even more so in json files, since they are probably visible from the browser unless you take specific precautions to prevent that. – GrumpyCrouton Feb 22 '22 at 15:37
  • @GrumpyCrouton this is for mid-term and database will be in final-term, so only json is available for me right now, can you please explain a bit about the thing you mentioned, storing json objects inside a json object – AbyssWalker Feb 22 '22 at 15:55
  • So you need an array of objects..... – epascarello Feb 22 '22 at 16:15

2 Answers2

1

I agree with @Konstantinos Gounaris. The json file is in wrong format. That's why the json_decode function returned nothing. Always use json_encode while writing the json file and write it in one flow. If you want to append to the file, just read the contents, decode it and write it again. Don't append to the file, because it will break the json format.

#Note: Saving confidential information to the file is wrong practice, which leads to security issues.

0

It is a better practice to use databases to store data(they are more secure than files). However. The first thing that i can see that is wrong is that you are decoding the json file into an php array and then you are writing this array into a file, this results into wrong Json format.

Bellow is an example of how you should do it:

$data_to_import = ['name'=>'John','surname'=>'Doe'];
$data_array = json_decode(file_get_contents($file), true);
array_push($data_array, $data_to_import);
$f = fopen("log", 'a');
fwrite($f, json_encode($data_array));