Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rpmcmurphy's avatar

Store visitor cart data for unregistered and registered users in API, not session

Built an API for an e-commerce app. Need to store cart items while user is not logged in (guest visitors). Could be a custom table for that purpose, or something else. The issue is, what is the standard way to identify a particular visitor to store the data as there is no user_id for unregistered visitors. I will need to store both registered and unregistered user's cart info.

In the web part of my application, session takes care of this. I don't need code example per se, just some ideas on how this particular situation is dealt with. If there is a standard solution. Thanks.

0 likes
2 replies
tykus's avatar

You can exchange a JWT between the client side and server-side applications to identify the user's "session" - the JWT can contain the authenticated user's ID if available.

rpmcmurphy's avatar

@tykus I eventually didn't use JWT in particular but used a similar process. On first add to cart request from the app, I basically generated an UUID for that particular client and stored it in the client's app storage. The UUID and other info is stored in the DB, along with user ID if available (if user is logged in). That way I can now track the cart data from the web too, in case I have the user ID (optional column). Worked out pretty well. Anybody interested-

Two tables- carts, cart_items. Cart table has the UUID column and the user_id column which is optional. Any product added, goes to the cart_items column with UUID, product_id, price, unit price etc. UUID works as the unique client identifier.

Please or to participate in this conversation.