Using sessions on the website

I started looking to re-write some pages on the clients website and I see that, for example, as soon as someone logs in, there are 53 Session variables .

It seems crazy to me to set everything up as soon as you log in and then all over the website to either go back to those sessions or add new ones.

More often than not, their site gets errors because one of the many sessions has lost its meaning.

So my question is the best way to reuse these pages.

  • Can this number of sessions continue to be used?
  • Is there a better alternative to using sessions?
  • There are some variables that are needed for ALL pages on the site, so is it better to retrieve them from the database on every page or set them in a session?

I believe only one session is needed and that is USERNAME / USER-ID and everything else should be grabbed from the database as needed.

Thank you in advance

+3


source to share


3 answers


It is generally not recommended to use session so heavily, but it depends on what data is stored there. If it's a bunch of strings and integers, this is probably not too important, but if they store robust objects and / or large datasets in a session, I would definitely consider refactoring them.

Generally, I try to avoid using a session for anything other than user-related data. It might be tempting to use the session for other purposes, but with good design and implementation, you don't really need.

Here are some guidelines on where to store things:



  • For user-specific data that is frequently used throughout the application, use a session to reduce the number of trips to the database

  • For data that is more or less static and independent of or related to the user, use cache or app state to reduce database trips

  • For page specific data, use ViewState and / or hidden fields. However, don't use ViewState to store LOBs or datasets

  • For data considered safe for users to view, consider using QueryString to pass information from page to page

The above principles tend to point you in the right direction, but as with all cases, there are exceptions. Just think about where you store the data and why you store it there, and you should be fine.

+3


source


First of all, I highly doubt you have multiple sessions per user, which means you are referring to session variables.



Second, it all depends on your goals, but if you have these many variables, perhaps you should use a DB instead to store and retrieve them when needed. 53 session variables are definitely WAY TOO MUCH for any website.

0


source


It depends on your situation, but I would go over and justify why you need each of the 53 session variables. While this is unheard of, you need to consider how much RAM will be stored in your session storage, as this can affect the number of concurrent users your site can support.

You can do as @walther suggests, but again I would like to get a rationale as placing them in the database will trade an in-memory session operation to call (network) the database. Maybe some of the values ​​are truly global and not session specific? In this case, you can transfer them to the Cache object.

Basically, research their use and come up with a suitable storage area. It's okay to leave them in Session if needed, but you need to understand the impact and consequences.

0


source







All Articles