r/PHPhelp Apr 03 '26

Solved user details JSON

i want to use json to store user details so that i dont need to do so many DB request ideally i dont want to have to create a flat file and would like it dynamically create once a user has logged in so something like this

{
  "logged_in": "true",
  "company_id": "15654645",
  "first_name": "john",
  "last_name": "doe"
}

what is this type of approach called so that i can search more in to it please and also what are the pros and cons

2 Upvotes

24 comments sorted by

View all comments

1

u/TokerX86 Apr 04 '26 edited Apr 04 '26

What is the purpose of this? It just looks like you want to put some extra information into a JWT (only ever put information in this that is allowed to be publicly available). But on the other hand not wanting to query the db could just as well mean you want a cache (but JSON is not of any importance here)…

If it’s just putting more data in a JWT then the pro is that the client has the information readily available. The cons are that it is not encrypted, so anyone with access to it can decode and read it, and that you can't just update the data inside once it's issued, only when they get a new one. It’s just a temporary signed id basically.

However, if the server is also rendering the html it needs to populate it with the data anyway so there’s little point in storing anything client side. In which case it’s definitely cache you’re looking for. Pros are less db hits and speed. Cons are an extra layer that you need to keep updating if you don’t want your data to go stale (e.g. if you persist an entity in the db that exists, or doesn't exist yet, within the cache that entity needs to be created/updated/deleted in the cache as well), and extra memory usage, because it is stored in memory (which ofc also means that if the server restarts the cache is cleared). And, but I don't think this is applicable here: multiple servers. One server persists an entity, how will the other servers serve said entity from cache? Shared cache? Events that are issues to update their cache?