SESSION TECHNIQUES IN .NET
Session is the time when the client and server interacts with each other. A session starts when the end user starts the browser and end when the end user closes the browser. For example let’s say user starts a site www.xyz.com then the session starts. After that let’s say he starts shopping, checking emails and closes the browser. Once he closes the browser the session terminates. In simple words server application communicates with your client browser in a STATELESS way.
Because of this nature of statelessness the end user who surfs your site can have undesirable results. For example take the example of the below interaction between server and client. See the last step where the user gets undesirable results. He was expecting a report but he is redirected to the login page.
So somehow we need to remind the server on which step or state we were , so that he can resume from that point. This is where we need to manage session.
Session management can be done by two ways:-
- Client session management (Cookies) :- In client session management the state is stored in the client browser in files called as cookies. So for example you can set the cookie with value which page you visited using the below javascript code.
document.cookie="lastpagevisited=login";
Now every time client visits the server the cookie value is sent reminding what is the state of the client so that server can work as per the state of the client. To retrieve cookie using ASP.NET server side programming language we need to use “Request” object and use the “Cookie” function to retrieve the value using the key.
Request.cookie(“LastPageVisited”)
So now if the client cookie says “LastPageVisited” was “Login” then the server should probably send him the “HomePage”.
If (Request.cookie(“LastPageVisited”) == “login”
{
Response.redirect(“Home”);
}
{
Response.redirect(“Home”);
}
- Server side session management (Session variables) :- Server side session management is done by using session variables. Session variables maintain data per user right from when the browser starts till the end. As soon as the browser is closed session variable is cleaned up. Below are the syntaxes to set session value and to get the value.
Session[“LastPage”] = “Login”;// to set session value
string strlastpage = Session[“LastPage”]; // get the session value
string strlastpage = Session[“LastPage”]; // get the session value
No comments:
Post a Comment