please bear with me for the explanation, it's necessary.
System background:
I am creating a parent->child navigation system for the backend of a CMS.
I build URLs like so: [domain.ext]/[moduleName]/[objectName]/[actionName]/[ID #1]/[ID #2]
.
The URL explained (e.g. example.com/PageManagement/Page/Modify/7/13
):
Navigation happens by parsing the URL for its components, reducing the names to system IDs etc., and then retrieving the fields and data which are to be displayed on the page.
To ensure the URL remains readable and logically understandable I don't add multiple layers of parent ID's to the URL, but rather keep the last few parent IDs in the PHP $_SESSION
array, so I can determine on navigation whether a different parent ID has to be added.
Example: image we have the objects Page->Extension->Field
, which are all in the module PageManagement
and parented from left to right. Now let's say we have a Page with ID 2, an Extension with ID 8, and a Field with ID 17. The URL for editing the Field would be example.com/PageManagement/Field/Modify/8/17
, because we are editing Field 17, which has Extension 8 as parent. The URL for editing the Extension would be example.com/PageManagement/Extension/Modify/2/8
, because we are editing Extension 8, which has Page 2 as parent. Editing the Page would simply be example.com/PageManagement/Page/Modify/2
, because it has no parent.
Problem:
Now, all of this works perfectly. However, if multiple tabs are opened they share the same $_SESSION
, so navigating in one tab can throw off the parent history for the other tab. It still goes right in almost all cases, but the fact I can cause it is bad (since someone can be adding/deleting/editing data without knowing they are actually in the wrong parent list).
What I need: I need a way, with each request, to determine from which tab it came, most likely by generating some form of UID per tab and sending that with each request. My system can then store the navigation history per tab, instead of per session.
Considered solutions
Window.sessionStorage
(which gets reset for each new window/tab). This would allow me to generate one if none is set yet (so a new tab), and thus have a different one stored (and remembered) per page session.
moduleName
.
If anyone can solve the problems mentioned for the solutions above, or come up with a completely new solution, that would be amazing. Obviously I'd prefer make as few changes as possible to how the URL system works, but if that is the only solution, so be it...
There's a basic flaw in your request design that's causing you all the trouble.
A system should strive to include all relevant information within a single request whenever possible. In your case, that means ditching the mechanism that "remembers" earlier requests (state..) in the $_SESSION
and instead pass all the information in the request. It can be in the URL ("address" and/or query string), the headers when appropriate (Usually using cookies. Probably inappropriate in this case) or body (same way as POSTed forms pass the payload).
There are numerous reasons to choose this path, to name a few:
As always, no rule goes without exceptions. In this case, the most common piece of information that is NOT suitable to be included in each request is authentication information (username, password, etc...)
To summarize, you should strongly consider reconstructing your requests such that all required information is available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With