Are Sessions stored server or client side ?

Last post 05-13-2008 4:32 PM by jimmy q. 8 replies.

Sort Posts:

  • Are Sessions stored server or client side ?

    05-13-2008, 4:47 AM
    • Loading...
    • Sleb
    • Joined on 02-19-2007, 8:31 PM
    • Posts 51

     Hello,

    I have a site where a user can create a list of goods.

    Each time someone creates a list, we store the listId in the session so they can add items to their list.

    The problem is,

    Say User1 created a list and we store the listId (value=1) in a session (called ListID). User 1 add items to his list and in the db we store the listId, item name ...

    Now comes User2, creates a list and gets a listId value of 2 that we store in a session (called ListID). He can also add his items ...

     
    Now User1, who is still logged in, adds another item, but when we get the value for ListID session, it passes 2 (value for User2) instead of 1.

     
    Are sessions stored server side or client side ?
     

    Any suggestions on how to fix that pb ?

     

    Thanks 

  • Re: Are Sessions stored server or client side ?

    05-13-2008, 4:53 AM
    Answer
    • Loading...
    • jimmy q
    • Joined on 11-02-2006, 5:01 AM
    • Australia
    • Posts 2,450
    • Moderator

    I read your post several times and I still cannot fully understand your problem, but in answering your question, session state is stored server side and is unique to each client session.

  • Re: Are Sessions stored server or client side ?

    05-13-2008, 4:59 AM
    • Loading...
    • Sleb
    • Joined on 02-19-2007, 8:31 PM
    • Posts 51

    Ok, this is what I don't understand. Mine doesn't seem to be unique to each client session.

    Basically when we request the session (ListID) we always get the newest one stored regardless of the client. So User1 gets the session value from User 2 , the both of them will get the value from User 3 ....

  • Re: Are Sessions stored server or client side ?

    05-13-2008, 5:02 AM
    Answer
    • Loading...
    • Mahadeomatre
    • Joined on 04-23-2007, 11:00 AM
    • Pune, India
    • Posts 228

    sessions are stored on server site, and for each session there is a unique id for each user / browser.

    it may be in ur code ur using some static fileds/ variables which are stored in session. and thats why ur getting wrong value.

    if ur using like

    static int key=listitems.selectedValue;

    session["listid"]=key;

    then its will give wrong value for each user.

    can u put ur code here to store the values in session.

     

    Thanks,
    Mahadeo

    -----------------------------------------------
    Best is Not the END point, but a starting point for Innovation
    ------------------------------------------------
    Remember to mark posts as the "Answer" to help future users.
  • Re: Are Sessions stored server or client side ?

    05-13-2008, 5:03 AM
    • Loading...
    • talgiladi
    • Joined on 07-01-2007, 9:06 AM
    • Israel
    • Posts 41

     The session is stored on the server side, and should be unique, meaning that what you've described shouldn't happen. You probably have some bugs in the code.

    Tal Giladi
    Software engineer
    www.talgiladi.net
  • Re: Are Sessions stored server or client side ?

    05-13-2008, 5:10 AM
    • Loading...
    • Deleo
    • Joined on 11-04-2007, 3:33 PM
    • Posts 135

    Are you testing this on a production server or on your localhost?

    If you use localhost in the URL the IExplorer skips alot of work, cheating if you like. So if you need to test session, open up two different IExplorer where one has the localhost adress and the other have your pc name in it. Iexplorer 1: localhost/mysite.aspx                     Iexplorer 2: MyPcName/mysite.aspx

    This way the explorer makes two different calls on your localhost and your session should be different :)

     

  • Re: Are Sessions stored server or client side ?

    05-13-2008, 9:53 AM
    • Loading...
    • Sleb
    • Joined on 02-19-2007, 8:31 PM
    • Posts 51

     Hello,

    Thanks for all the replies so far. I did test it across multiple browsers and servers so it must be something wrong in my code.

    The code is below. I have a class called session manager

    public class SessionManager
        {
            public SessionManager()
            {
            }

             public static string ListId
            {
                get
                {
                    return (string)HttpContext.Current.Session["ListId"];
                }
                set
                {
                    HttpContext.Current.Session["ListId"] = value;
                }
            }
     }

     

    Then in my code behind

     

    private static string listId

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (SessionManager.ListId== null)
                    {
                        Response.Redirect("~/List/Create/");
                    }
                    else
                    {
                        listId = SessionManager.ListId;               
                    }
                }
            };

     

    Thanks 

  • Re: Are Sessions stored server or client side ?

    05-13-2008, 12:33 PM
    • Loading...
    • linjitvd
    • Joined on 04-02-2008, 2:57 PM
    • Posts 17

    As Mahadeomatre  said, don't use static. It reserves the status for the whole application.

  • Re: Are Sessions stored server or client side ?

    05-13-2008, 4:32 PM
    Answer
    • Loading...
    • jimmy q
    • Joined on 11-02-2006, 5:01 AM
    • Australia
    • Posts 2,450
    • Moderator

    Sleb:
    public static string ListId
            {
                get
                {
                    return (string)HttpContext.Current.Session["ListId"];
                }
                set
                {
                    HttpContext.Current.Session["ListId"] = value;
                }
            }
     

    Be really careful when using the keyword static. Static members exist throughout the entire application domain so simply put, it because global so that is why you session data is no longer unique.

     

Page 1 of 1 (9 items)