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

gitwithravish's avatar

How to keep track of selected user resource and show content accordingly?

I have came across such projects a lot where a user has many resources, and user is shown a dashboard according to the resource he has selected. For example, I worked for a company that has multiple hotels in India. They wanted a CMS system in which they can add a hotel and then manage that particular hotel by adding different details defined in different section of the dashboard. My question is how to keep track of selected hotel.

1 Pass hotel id as GET parameter

domain.com/dashboard/hotel-images?id=1342

2

Pass hotel id/slug as route parameter

e.g. domain.com/hotel-1/dashboard/hotel-images

3

Store selected hotel id/details in session

4

Whenever user selects a hotel, make a controller to add fresh timestamp to hotel table's last_accessed_at field and then whenever a user tries to access a page, just look for a hotel with most recent value in that column. This way, when user loges in again in the future, he will get the last accessed hotel dashboard opened by default, which wont be possible with sessions.

Which approach is a good practice? Or is there any better way to deal with this?

0 likes
16 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

I would go with option 2. I have a legacy project that does a mix of 2 and 3. Those places where it uses option 2 are very good. Lets say one of our users has an issue, the person can just send me the url. (same with user to use)

In this project I often see people send around urls like http://example.com/admin/report/edit with a text like "Please set up new reports in the same way as the report listed here".. But as it is session based the recipients have no clue what report is being talked about

For companies (customers) we use the url approch http://example.com/admin/company/22/edit meaning people can share this link and we will always see the same page.

gitwithravish's avatar

@sinnbeck That makes sense ! Can you tell me, with this approach, how can I open dashboard home page with the property that was selected previously? Is it a good practice to combine option 2 and 4?

Sinnbeck's avatar

We have a table that keeps track of what has been opened previously opened reports and companies (a table for each). Both lists are always on screen, and redirecting to the last one is easy :)

gitwithravish's avatar

@sinnbeck Okay but how do I add an entry to that table? Should I put an entry in that table before loading any hotel page? And how the table structure should be?

Sinnbeck's avatar

We do it as you go to the page. Delete all rows where user ID and hotel ID match, and add a new record

And the table has two columns, user_id and report_id

gitwithravish's avatar

I am unable to understand the need of report_id here. All I want is to keep track of which hotel the user accessed most recently. We do not have anything related to reporting. Can you explain me how can I implement it for my project ? I am sorry if I am unable to understand you @sinnbeck

Snapey's avatar

hotel is a resource

keep the resource id in the url whilst you are working with it.

so 'dashboard' is the hotels' dashboard. It does not matter that the same view is shared by every hotel.

I would opt for a scheme like;

/hotels/3253/dashboard

/hotels/3253/dashboard/images/

oh, also, don't use session or last accessed because it screws anyone that tries to work with two hotels at the same time in different tabs.

gitwithravish's avatar

@snapey That last line poked me. If I am using sessions, will the following scenario screw up my application the way I think it will?

User opens hotel images page for hotel 1

selected hotel in session is set to 1

then user opens same page for hotel 2

selected hotel in session is set to 2

now user makes a post request to upload an image in hotel 1 which is open in first tab.

but the image will be added for hotel 2 because hotel 2's ID was in the session . M I correct😲 ??????

Sinnbeck's avatar

Yes. Another reason to use url instead of session :)

1 like
Snapey's avatar

If you rely on the last value written to the session, then yes.

If the user opens a new tab then it shares the session.

I made the decision to use session for filters on my site https://speakernet.co.uk. Try it. Open one tab in your browser.

click on the orange button next to a talk for 'Life Skills' you will see 118 talks listed. Open a new tab for the same site.

Click on 'Browse Talks' in the menu bar. You will note that lifeskills is already selected and your results are 118.

Proves that the session is shared between tabs.

1 like
gitwithravish's avatar

Thank you @sinnbeck @snapey Now last thing. Can anyone tell me how do I open the hotel by default, the next time user logs in🙃 ? @sinnbeck explained it earlier in the comment section but I did not understand that.

Snapey's avatar

With a cookie perhaps?

Alternatively give the user some form of status record and save the hotel there each time they change it.

Sinnbeck's avatar

As I said, you can save it to the database.

When user logs in, you redirect to a controller::method that is just there to check where to send the user.

The controller checks the database for the last entry from the user, and redirect them there. If none is found the user is redirected to the dashboard (or something like that)

gitwithravish's avatar

Okay thanks everyone ! I found what i was looking for and got to learn few new things.

@davidifranco Would you mind removing your comments because they are irrelevant to the topic and may confuse future viewers.

Thanks.

Please or to participate in this conversation.